views:

161

answers:

4

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
+3  A: 

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)

Bobby Jack
I think he wanted the answer in awk. Also, that grep needs to be for '^\['.
whaley
Good point on the grep - will edit. I'm not sure about the awk restriction though - it's mentioned in the title, but not the question itself. I can't think why awk would be required, other than as a learning exercise - which is valid, of course.
Bobby Jack
I +1'ed you for the simplicity and usefulness of the answer anyway. cut is much easier to read/write for this case.
whaley
+2  A: 

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
whaley
+1  A: 

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;
}
mjv
A: 
awk -F"[][]" 'NR>2{print $2}' file
ghostdog74