Sunday, June 7, 2009

Using grep and cut

Grep and cut are some of the tools used for text processing. They use regular expressions to filter out specific terms in a file


Task :
To find out the IP Address from Ifconfig

Solution :
ifconfig eth0 | grep "inet addr" | cut -d ":" -f 2 | cut -d "B" -f 1

Explanation :
1. ifconfig eth0 (Displays network settings for your pc)
2. grep "inet addr" (Filters lines to the line containing string "inet addr")
3. cut -d ":" -f 2 (Cuts the obtained line into columns using delimiter ":" and then displays the 2nd column)
4. cut -d "B" -f 1(Cuts the obtained line into coumns using delimiter "B" and then displays the 1st column)