Time To Earn

Saturday, November 30, 2013

Daily Weapons of Unix in Office

Count the number of occurances of a pattern in a file using awk
awk '{ for (i=1;i<=NF;i++) if ( $i == "word" ) count++ } END{print count}' filename
This will print the count of the occurance of  a pattern "word" in a file.


How to compare the values of a column in awk in a same file and consecutive lines..
If one would like to compare the values of 2nd column of consecutive lines of same file in such a way so that if the difference between first value and second value is more than 100 it should print complete line else ignore line.

Input File:
=======

ABC 2500
ABCD 123
XYZ 122
WXYZ 2565


Desired Output:
==========

ABC 2500 (i.e. difference between 2500 and 123 is greater than 100 here)
XYZ 122 (i.e. difference between 122 and 2565 is greater than 100 here)

Command:
=======

awk 'NR % 2 != 0 {a=$1; b=$2} NR % 2 == 0 {if (b - $2 > 100 || $2 - b > 100){print a,b}}' inputfile




Count the number of files in each directory recursively
find ./ -type d -exec sh -c "echo -n {} ' ' ; ls -l {} | wc -l" \;


Use awk in following cases

Joining two files parallely using awk:
========================
awk 'NR==FNR{a[NR]=$0; next} {print a[FNR], $0}' file1.txt file2.txt

Changing extension of all .txt to .sh:
========================
for i in *.txt;do mv "$i" "${i%.txt}".sh;done



Print 3 lines before and 5 lines after pattern match abcd in a file a.txt
In Linux:

awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=5 s="abcd" a.txt



ftp in UNIX
ftp -n 10.200.120.12 << EOF
quote user username
quote pass password
cd /path/to/directory/
prompt off
put dump.txt
exit
EOF




Print one line before pattern match using sed in UNIX.
sed -n -e '/manu/{x;p;x;p;}' -e h test.txt




sort a delimited file on the basis of a column
sort -t":" -k3 filename.txt

(The above will sort a file filename.txt which is ":" delimited on the basis of third column.)




Print every 6th line of a file ..
The below awk command will print every 6th line of a file test.txt starting from line number one.

awk '{if(NR%6==1) print $0}' test.txt

(Note: Print every nth line using awk.)



# insert a blank line above every line which matches "regex"
 sed '/regex/{x;p;x;}' OR  sed '/regex/{x;G;}'

# insert a blank line below every line which matches "regex"
 sed '/regex/G'

# insert a blank line above and below every line which matches "regex"
 sed '/regex/{x;p;x;G;}'

# remove the header of a file
sed '/1,1d' filename


No comments:

Post a Comment