Linux sorting, crontab & concat
Linux: Sorting a File
[root@myhost tmp]# cat testSort.txt
23
4
56
001
34
3
To sort it:
[root@myhost tmp]# sort -u testSort.txt > sortd_testSort.txt
wherein, -u –> Unique
Its sorted:
[root@myhost tmp]# cat sortd_testSort.txt
001
23
3
34
4
56
Oops! Though it worked, it didn’t work correctly. Because the file contains numeric data.
So use the below:
[root@myhost tmp]# sort -u -n testSort.txt > numeric_sortd_testSort.txt wherein, -n –> for numeric data
And, now its correct:
[root@myhost tmp]# cat numeric_sortd_testSort.txt
001
3
4
23
34
56
However, this works only for the small files. Smaller than space available with ‘/tmp’ partition.
If you want to sort huge files, use the below:
[root@myhost lib]# sort -u -n -T. testSort.txt > numeric_sortd_testSort_new.txt wherein, -T. –> T indicates the location used as temporary space. By default its ‘/tmp’.
‘.’ indicates current directory
Linux: Crontab – Brief
For those who new to ‘cron’, its nothing but, an event scheduler in Linux. That means, you can schedule any script to run at any time you wanted to. Its just the system/server should be up and running!cron job is specific to every user in Linux/Unix. So, one can’t see other’s cron unless the necessary privileges or sudo root access given.
Whatever, here is the options in cron:
To check cron jobs:
[root@localhost kiran]# crontab -l
no crontab for root
Every Cron job should be given with 5 options:
- minute -> 0-59
- hour -> 0-23
- day of month -> 1-31
- month -> 1-12
- day of week -> 0-7 (0 is Sunday )
Linux: Concatenate In Linux
Consider a file as below:
[root@myserver 6048]# cat test.txt
12
0
123445456
234565443
3883
33
My requirement is to generate update/delete statement for each of this value. Think of a file with millions of records.
My update statement would be:
update mytable set col1=’Yes’ where col2=’ value in the file‘;
Here’s the solution:
[root@myserver 6048]# cat test.txt | sed “s/^/update mytable set col1=’Yes’ where col2=’/g;s/$/’;/g”
update mytable set col1=’Yes’ where col2=’12’;
update mytable set col1=’Yes’ where col2=’0′;
update mytable set col1=’Yes’ where col2=’123445456′;
update mytable set col1=’Yes’ where col2=’234565443′;
update mytable set col1=’Yes’ where col2=’3883′;
update mytable set col1=’Yes’ where col2=’33’;
update mytable set col1=’Yes’ where col2=”;
update mytable set col1=’Yes’ where col2=’987676′;
You may redirect the output to some other file, as below:
[root@myserver 6048]# cat test.txt | sed “s/^/update mytable set col1=’Yes’ where col2=’/g;s/$/’;/g” > updates_test.sql
You may also use the below query for the purpose:
[root@myserver 6048]# sed “s/^/update mytable set col1=’Yes’ where col2=’/g;s/$/’;/g” test.txt > updates_test.sql