Reputation: 119
I want to format below un-formatted inputs. I would like to print all the fields if the second column begins with "2".
Actual file contains many columns. Is there any easiest way to print all columns instead of typing print $2,$3,$4,$5,...$n
?
Input.csv
---------------------------
|Data statistics|Number of|
|-------------------------|
|Records passed | 750,517|
---------------------------
Transaction Data for Serial Numbers
No of Records : 750517
-----------------------------------------------------------------------------------------------------------
|Material Number|Serial Number From |Serial Number To |Document Year|Delivery Document|Material Document|
|----------------------------------------------------------------------------------------------------------
|200965 |18604165309338 |18604165309387 |2012 |6100202749 |AAA2778202 |
|201163 |10740000822407 |10740000822606 |2012 |6100202749 |AAA2778202 |
|201232 |18604177741067 |18604177741366 |2012 |6100202749 |AAA2778202 |
|201295 |18604221522337 |18604221523836 |2012 |6100202749 |AAA2778202 |
|201480 |18604113309952 |18604113310131 |2012 |6100202749 |AAA2778202 |
|201781 |18604199150436 |18604199150835 |2012 |6100202749 |AAA2778202 |
|201480 |6001400030046472 |6001400030046771 |2012 |6100202520 |AAA2777953 |
|202853 |6001700000180323 |6001700000180722 |2012 |6100202520 |AAA2777953 |
-----------------------------------------------------------------------------------------------------------
I am unsing below command :
awk ' BEGIN {FS ="|"; OFS = ","} { if ($2~"2") print $2,$3,$4,$5,$6,$7 }' Input.txt
Desired Output:
200965 ,18604165309338 ,18604165309387 ,2012 ,6100202749 ,AAA2778202
201163 ,10740000822407 ,10740000822606 ,2012 ,6100202749 ,AAA2778202
201232 ,18604177741067 ,18604177741366 ,2012 ,6100202749 ,AAA2778202
201295 ,18604221522337 ,18604221523836 ,2012 ,6100202749 ,AAA2778202
201480 ,18604113309952 ,18604113310131 ,2012 ,6100202749 ,AAA2778202
201781 ,18604199150436 ,18604199150835 ,2012 ,6100202749 ,AAA2778202
201480 ,6001400030046472 ,6001400030046771 ,2012 ,6100202520 ,AAA2777953
202853 ,6001700000180323 ,6001700000180722 ,2012 ,6100202520 ,AAA2777953
Upvotes: 1
Views: 135
Reputation: 203209
$ awk 'gsub(/\|/,",") && gsub(/^,|,$/,"") && /^2/' file
200965 ,18604165309338 ,18604165309387 ,2012 ,6100202749 ,AAA2778202
201163 ,10740000822407 ,10740000822606 ,2012 ,6100202749 ,AAA2778202
201232 ,18604177741067 ,18604177741366 ,2012 ,6100202749 ,AAA2778202
201295 ,18604221522337 ,18604221523836 ,2012 ,6100202749 ,AAA2778202
201480 ,18604113309952 ,18604113310131 ,2012 ,6100202749 ,AAA2778202
201781 ,18604199150436 ,18604199150835 ,2012 ,6100202749 ,AAA2778202
201480 ,6001400030046472 ,6001400030046771 ,2012 ,6100202520 ,AAA2777953
202853 ,6001700000180323 ,6001700000180722 ,2012 ,6100202520 ,AAA2777953
To address your comment below:
$ awk 'gsub(/\|/,",") && gsub(/^,|,$/,"") && $3~/^,600/' file
201480 ,6001400030046472 ,6001400030046771 ,2012 ,6100202520 ,AAA2777953
202853 ,6001700000180323 ,6001700000180722 ,2012 ,6100202520 ,AAA2777953
$ awk -F, 'gsub(/\|/,",") && gsub(/^,|,$/,"") && $3~/^600/' file
201480 ,6001400030046472 ,6001400030046771 ,2012 ,6100202520 ,AAA2777953
202853 ,6001700000180323 ,6001700000180722 ,2012 ,6100202520 ,AAA2777953
Upvotes: 4
Reputation: 16997
This may help you
$ cat file
---------------------------
|Data statistics|Number of|
|-------------------------|
|Records passed | 750,517|
---------------------------
Transaction Data for Serial Numbers
No of Records : 750517
-----------------------------------------------------------------------------------------------------------
|Material Number|Serial Number From |Serial Number To |Document Year|Delivery Document|Material Document|
|----------------------------------------------------------------------------------------------------------
|200965 |18604165309338 |18604165309387 |2012 |6100202749 |AAA2778202 |
|201163 |10740000822407 |10740000822606 |2012 |6100202749 |AAA2778202 |
|201232 |18604177741067 |18604177741366 |2012 |6100202749 |AAA2778202 |
|201295 |18604221522337 |18604221523836 |2012 |6100202749 |AAA2778202 |
|201480 |18604113309952 |18604113310131 |2012 |6100202749 |AAA2778202 |
|201781 |18604199150436 |18604199150835 |2012 |6100202749 |AAA2778202 |
|201480 |6001400030046472 |6001400030046771 |2012 |6100202520 |AAA2777953 |
|202853 |6001700000180323 |6001700000180722 |2012 |6100202520 |AAA2777953 |
-----------------------------------------------------------------------------------------------------------
Output
$ awk 's=$1~/^\|2/{gsub(/^\||\|$/,""); gsub(/\|/,",")}s' file
200965 ,18604165309338 ,18604165309387 ,2012 ,6100202749 ,AAA2778202
201163 ,10740000822407 ,10740000822606 ,2012 ,6100202749 ,AAA2778202
201232 ,18604177741067 ,18604177741366 ,2012 ,6100202749 ,AAA2778202
201295 ,18604221522337 ,18604221523836 ,2012 ,6100202749 ,AAA2778202
201480 ,18604113309952 ,18604113310131 ,2012 ,6100202749 ,AAA2778202
201781 ,18604199150436 ,18604199150835 ,2012 ,6100202749 ,AAA2778202
201480 ,6001400030046472 ,6001400030046771 ,2012 ,6100202520 ,AAA2777953
202853 ,6001700000180323 ,6001700000180722 ,2012 ,6100202520 ,AAA2777953
Upvotes: 1
Reputation: 289505
You can say:
$ awk -F"|" -v OFS="," '$2~/^2/ {NF--; $0=$0; print}' file
,200965 ,18604165309338 ,18604165309387 ,2012 ,6100202749 ,AAA2778202
,201163 ,10740000822407 ,10740000822606 ,2012 ,6100202749 ,AAA2778202
,201232 ,18604177741067 ,18604177741366 ,2012 ,6100202749 ,AAA2778202
,201295 ,18604221522337 ,18604221523836 ,2012 ,6100202749 ,AAA2778202
,201480 ,18604113309952 ,18604113310131 ,2012 ,6100202749 ,AAA2778202
,201781 ,18604199150436 ,18604199150835 ,2012 ,6100202749 ,AAA2778202
,201480 ,6001400030046472 ,6001400030046771 ,2012 ,6100202520 ,AAA2777953
,202853 ,6001700000180323 ,6001700000180722 ,2012 ,6100202520 ,AAA2777953
Upvotes: 2