user356393
user356393

Reputation: 31

AWK scripting :How to remove Field separator using awk

Need the following output

ONGC044 
ONGC043
ONGC042
ONGC041
ONGC046
ONGC047

from this input

Medium Label                   Medium ID                            Free Blocks
===============================================================================
[ONGC044] ECCPRDDB_FS_43       ac100076:4aed9b39:44f0:0001            195311616
[ONGC043] ECCPRDDB_FS_42       ac100076:4aed9b1d:44e8:0001            195311616
[ONGC042] ECCPRDDB_FS_41       ac100076:4aed9af4:4469:0001            195311616
[ONGC041] ECCPRDDB_FS_40       ac100076:4aed9ad3:445e:0001            195311616
[ONGC046] ECCPRDDB_FS_44       ac100076:4aedd04a:68c6:0001            195311616
[ONGC047] ECCPRDDB_FS_45       ac100076:4aedd4a0:6bf5:0001            195311616

Upvotes: 3

Views: 3261

Answers (4)

ghostdog74
ghostdog74

Reputation: 342303

awk -F"[][]" 'NR>2{print $2}' file

Upvotes: 6

Bobby Jack
Bobby Jack

Reputation: 16018

If the data is really that uniform, a very simple pipe to:

cut -b 2-8

will do it for you.

(Oh, apart from the first two lines; get rid of them with grep ^\[ in your pipeline, if you need to)

Upvotes: 3

mjv
mjv

Reputation: 75095

Here's a solution which only involves awk. The idea is to let awk parse the desired column and remove the undesired square brackets from this token. The only "trick" is to "escape" the [ character lest it is understood as an opening re set. (We could also use substr instead since the brackets are expected as the first and last characters)

{
     #skip the column header lines
     if (NR < 3)
       next;

     # $1 is almost what we want: [xxxx] 
     #   ==> just remove the square brackets
     sub("[[]", "", $1);
     sub("]", "", $1);
     print $1;
}

Upvotes: 1

whaley
whaley

Reputation: 16265

I think what you want is:

awk '/^\[.*\]/  {print substr($1,2,7)}' < ~/tmp/awk_test/file

This assumes that your first field is exactly 9 (7 of them are the ones you want) characters each time. If this is not the case, then use the following instead to strip off of the [ and the ] from the first field:

awk '/^\[.*\]/  {gsub(/[\[\]]/,"",$1); print $1 }' < ~/tmp/awk_test/file

Upvotes: 3

Related Questions