Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using awk on a string

Tags:

linux

bash

awk

can I use awk to extract the first column or any column on a string? Actually i am using a file and reading it to a variable I want to use AWK on that variable and do my job.

How is it possible? Any suggestions.

like image 260
monucool Avatar asked Jun 09 '12 07:06

monucool


1 Answers

Print first column*:

<some output producing command> | awk '{print $1}'

Print second column:

<some output producing command> | awk '{print $2}'

etc.

Where <some output producing command> is like cat filename.txt or echo $VAR, etc.

e.g. ls -l | awk '{print $9}' extracts the ninth column, which is like an ... awkward way of ls -1

*Columns are defined by the separating whitespace.


EDIT: If your text is already in a variable, something like:

VAR2=$(echo $VAR | awk '{print $9}')

would work, provided you change 9 to the desired column.

like image 159
jedwards Avatar answered Oct 05 '22 18:10

jedwards