Saturday, January 12, 2013

Some fun with cut, tr, awk


List the names of the files,
[jay@sama ~]$ ls -l | cut -d" " -f9 | more

note the output..

To fix it, you have to squeeze the spaces to single space.
[jay@sama ~]$ ls -l | tr -s " " | cut -d " " -f9

note the output now.

[jay@sama ~]$ ls -l /etc | sort | head | cut -d " " -f1 | more

see the output,

[jay@sama ~]$  ls -l /etc | tr -s " " | sort | head | cut -d " " -f 1,2,3,4,5,6,9 | more

notice the output.

Now, try this,
[jay@sama ~]$ ls -l | awk '{ print $1 $2 $3 }'

[jay@sama ~]$ ls -l | awk '{ print $1 " " $2 " " $3 }'

[jay@sama ~]$ ls -l | awk '{ print $1 "\t" $2 "\t" $3 }'

Try this,

[jay@sama ~]$ cat mylist.txt
John Smith, 12 Smith Dr, Fairfax VA
Bill Johnson, 24 Douglas Dr, Omaha, NE
Peter Stewart, 55 Wyane st, Dallas, TX

[jay@sama ~]$ awk -F, '{ print $1 $2 $3 }' mylist.txt

[jay@sama ~]$ awk -F, '{ print $1; print $2; print $3 }' mylist.txt

[jay@sama ~]$ awk -F: '{ print $1 "\t" $7 }' /etc/passwd | more

[jay@sama ~]$ cat mylist.txt | tr [a-z A-Z]

No comments:

Post a Comment