AWK scripting: how to remove field separator using awk

The following conclusion is required

ONGC044 
ONGC043
ONGC042
ONGC041
ONGC046
ONGC047

from this entrance

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
source share
4 answers
awk -F"[][]" 'NR>2{print $2}' file
+6
source

If the data is really the same, a very simple channel:

cut -b 2-8

will do it for you.

(Oh, except for the first two lines, get rid of them using grep ^\[in your pipeline if you need to)

+3
source

, :

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

, 9 (7 - , ) . , [] :

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

There is a solution here that includes awk. The idea is to awk parse the desired column and remove unwanted square brackets from this token. The only “trick” is to “avoid” the symbol [so that it is not understood as a discovery. (We could also use substr, 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;
}
+1
source

Source: https://habr.com/ru/post/1748151/


All Articles