Krishna Logo
qa training in canada now
Divied
Call: Anusha @ 1 (877) 864-8462

 

Latest News
Home Navigation Divied
INTERVIEW Navigation Divied UNIX INTERVIEW QUESTIONS Navigation Divied How to get the n-th field from a UNIX command output?
How To Get The N-th Field From A UNIX Command Output?
How to get the n-th field from a UNIX command output?

Ans: We know we can do it by [cut]. Like below command extracts the first field from the output of [WC –c] command
$>WC -c file.txt | cut -d' ' -f1
109
But I want to introduce one more command to do this here. That is by using [awk] command. [awk] is a very powerful command for text pattern scanning and processing. Here we will see how may we use of [awk] to extract the first field (or first column) from the output of another command. Like above suppose I want to print the first column of the [wc –c] output. Here is how it goes like this:
$>wc -c file.txt | awk ' ‘‘{print $1}'
109
The basic syntax of [awk] is like this:
awk 'pattern space''{action space}'
The pattern space can be left blank or omitted, like below:
$>wc -c file.txt | awk '{print $1}'
109
In the action space, we have asked [awk] to take the action of printing the first column ($1). More on [awk] later.


Shadow Bottom
 
 
© 2005 -