Manipulate and move columns in a file

Hello Unix Gurus,

I have a request 2 perform several functions on a file, delete columns, delete rows based on column value, and finally move around columns in the final output. Consider the following input file with 12 columns;

NETW~US60~000000000000518000~008~~CS~USD~102.00~20090701~99991231~01~01~20080714
NETW~US60~000000000000518000~SL8~~CS~USD~102.00~20100701~99991231~01~01~20080714
NETW~US61~000000000000772000~008~~CS~USD~118.08~20090701~99991231~01~01~20051012
NETW~US60~000000000000772000~SL8~~CS~USD~118.08~20100701~99991231~01~01~20051012
NETW~US60~000000000000772000~008~~CS~USD~118.08~20100701~99991231~01~01~20051012
  1. The first action I want to perform is to delete columns 1, 10, 11, and 12
  2. Delete all rows where value in column 1 is not equal to US60 and column 3 is not equal to 008
  3. Convert values in column 1 from US60 to A
  4. Convert values in column 3 from 008 to SLP
    5 Move column 8 to column 4, move column 7 to column 5, move column 4 to column 8, move column 5 to column 7, column 6 stays in the same position.

Such that the output from all this would be:

A~000000000000518000~SLP~99991231~20090701~102.00~USD~CS~
A~000000000000772000~SLP~99991231~20100701~118.08~USD~CS~

Any script or code that can help achieve this results, will be greatly appreciated.

Kind Regards,

Chukwuma

Try...

awk 'BEGIN{FS=OFS="~"}$2=="US60"&&$4=="008"{print "A",$3,"SLP",$10,$9,$8,$7,$6,$5,""}' file1 > file2
1 Like