|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Question on script using awk
2131100 COVS/100C E|FWP AREAS|MIDWEST|
2131100 COVS/100C E|FWP AREAS|CONSTRUCTION| 2131100 COVS/100C E|FWP AREAS|FTC| 2131100 COVS/100C E|FWP AREAS|FWP AREAS| 2131100 COVS/100C E|FWP AREAS|CLEANING| 2131100 COVS/100C E|FWP AREAS|FCRE| 2131200 COVS/100C UPS|UPS ROOMS|CONSTRUCTION| 2131200 COVS/100C UPS|UPS ROOMS|UPS ROOMS| 2131200 COVS/100C UPS|UPS ROOMS|FCRE| 2131200 COVS/100C UPS|UPS ROOMS|EMERGENCY| I am running a report that produces the output shown above. I am trying to use awk in the script to format the output to look like this 2131100 COVS/100C E|FWP AREAS|MIDWEST|CONSTRUCTION|FTC|FWP AREAS|CLEANING|FCRE| All information would be on one line but the carriage return was forced when typing into the area for this post. Basically, I want to keep only the first occurence of the information in colums 1 and 2. I then want to list the information in column 3 from left to right in the same row. I want to do this for each new number listed, ie: one row that looks like the example above for 2131100 another row for 2131200 etc... Attached below is a copy of the AWK script which does not seem to be working properly. Any insight on how to accomplish this would be greatly appreciated. awk -F"|" 'BEGIN {SS_A=""} {if (NR==1) {SS_A=$1; printf $0} else {if (SS_A==$1) {printf "|"$2} else {SS_A=$1; printf "\n"$0} } } END {printf "\n"}' $SF_OUT |
|
#2
|
|||
|
|||
|
The following works for me:
Code:
#!/usr/bin/awk -f
BEGIN {
FS="|"
line=""
}
$1 $2 == line {
printf $3 FS
next
}
{
if (line != "")
print ""
printf $0
line=$1 $2
}
END {
print ""
}
|
|
#3
|
|||
|
|||
|
reply to awk code
thank you for the response. Any idea why the script suggested would be producing output that contains 2 occurences of the 2nd field?
Any thoughts are welcome. Also, if an example of input and/or output would be helpful I can provide them. Thank you. |
|
#4
|
|||
|
|||
|
Please do provide an example of the input you used that caused a problem, as well as the output you saw. The output I get from the example input you posted originally is:
Quote:
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Question on script using awk |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|