Home > Software design >  Extract the one word and one amount field after pattern matching for the two strings in the same lin
Extract the one word and one amount field after pattern matching for the two strings in the same lin

Time:01-12

Extract the one word and one amount field after pattern matching for the two strings in

the same line for the one file

first word needs to be extracted after SEC and before ::

Amount field needs to be extracted after AVAI:

Input:

10A:SEC SECRTY::security tip:10B::AVAI:3000,:10c:RUI
10A:SEC KGTY::STAR Gather ltd:10B::AVAI:90000,:10c:SUR
10A:SEC VYTRIY::Query bench:10B::AVAI:2400,:10c:VYT

output :

SECRTY,3000
KGTY,90000
VYTRIY,2400

tried using awk command

CodePudding user response:

With sed substitution:

$ sed -E "s/.*SEC ([^:] ).*:AVAI:([^,] ).*/\1,\2/g" test.txt

SECRTY,3000
KGTY,90000
VYTRIY,2400

CodePudding user response:

This awk will do it. Set field separator to ':', set output field separator to ','. For column 2, delete everything up to the space inclusive. For column 8, delete the trailing comma. Then print columns 2 and 8.

$ awk -F':' 'BEGIN { OFS = ","} {gsub(/^.* /, "", $2); gsub(/,?$/, "", $8); print $2,$8}' file.txt
SECRTY,3000
KGTY,90000
VYTRIY,2400
$
  • Related