ps-aux
ps-aux

Reputation: 12176

Awk - return nth word in a column starting with pattern

I know I can use awk to get a line starting with expression exp by awk '/^exp/'. How do I get the n-th word from this line?

Upvotes: 0

Views: 555

Answers (3)

Kent
Kent

Reputation: 195199

give this a try:

awk -v n="$var" '/^exp/{print $n}' file

$var could be shell variable, the value is the index of your column.

Upvotes: 2

Pratham
Pratham

Reputation: 1723

Use {print $n} in the awk statement

echo "abcd bcd cd d" | awk '/^ab/ { print $3}'

This would echo "cd"

Upvotes: 3

Anoop
Anoop

Reputation: 5720

awk '{print $n}' to print out the nth word in the line ?

Upvotes: 1

Related Questions