Vikakmis
Vikakmis

Reputation: 13

awk : print two Fields with seperate number

I am new to unix and required some help...

I have a file with contents like:

119000 ABC/CSD/NEW/PB/PB1234_PB0001123.CSV
60000 ABC/CSD/NEW/PB/PB14567_PB0001123.CSV
25000 ABC/CSD/NEW/PB/VV/PB16734_PB0001123.CSV
80000 ABC/CSD/NEW/PB/VV/PB2314_PB09820123.CSV
33117 ABC/CSD/NEW/PB/VV/PB45634_PB0001123.CSV

I want output like:

119000 PB1234  PB0001123
60000 PB14567 PB0001123 
25000 PB16734 PB0001123 
80000 PB2314  PB09820123 
33117 PB45634 PB0001123 

Sorted by second field and then by third field...

Upvotes: 1

Views: 43

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133458

If your actual Input_file is same as shown sample then following awk may help you on same.

awk -F'[ /_.]' '{print $1,$(NF-2),$(NF-1)}'  Input_file

Explanation:

-F'[ /_.]': -F is used in awk for setting delimiter for lines. So here we are setting (space),/,_ and . as field separators for any line for mentioned(passed) Input_file.

print $1,$(NF-2),$(NF-1): Using awk's out of the box command named print to print then $1 mentions as first field value of current line, $(NF-2) mentions 3rd last field value of current line $(NF-1) indicates 2nd last field value of the current line.

Upvotes: 3

oliv
oliv

Reputation: 13249

You can try this:

awk '{
       split($2,a,"/")              # Split the 2nd element into the array a
       split(a[length(a)],b,"[_.]") # Split the last element of the array into the array b
       print $1,b[1],b[2]           # Print the wanted string
    }' file

Upvotes: 1

Related Questions