Tuesday, 1 June 2010

Linux command - cut, sed, awk

Cut

cut -c 1-7 f1 will output the first 7 characters in each line of file 'f1'.

cut -c 2,4-7,10- f1 will output the character 2, characters 4 – 7, characters 10 until the end of line in each line of file 'f1'.

cut -f 1,4,5 f1 will output 'fields' 1, 4 and 5 in file 'f1'. The fields are assumed to be separated by tab in 'f1'.

cut -d ' ' -f 1,4,5 f1 will output 'fields' 1, 4 and 5 in file 'f1'. The fields are assumed to be separated by a single space in 'f1'.

Sed

Let's make a test file
ls -l /etc > testsed

"d" means delete
sed '1,4d' testsed
sed '/yum/d' testsed
sed '/yum/!d' testsed

"s" is replace, -n means hide the other output, p is display
sed -n 's/pass/xxxxx/p' testsed


Awk
Make another file to test awk
ps aux > testawk

Show column 11
awk '{print $11}' testawk

awk '/sbin/{print $2,$11}' testawk

No comments:

Post a Comment