Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Joining lines in a file

Status
Not open for further replies.

HHG

Technical User
Nov 8, 2003
68
GB
Hi All,

Can anyone help I am pain stakingly trying to go through a file and in vi trying to join to line together. But the file is rather large there must be an easier way to do this.

e.g file :-

2226697 29/01/08
2226697 "630511" "0P0705011" 1.00
2226698 29/01/08
2226698 "630571" "0P0705071" 1.00
2226744 29/01/08
2226744 "396001" "0C2660001" 1.00
2226756 29/01/08
2226756 "630101" "0P0701001" 1.00
2226789 29/01/08
2226789 "620113" "0P0601013" 1.00
2226799 29/01/08
2226799 "815005" "0E0115005" 1.00
2226799 "140009" "0B0840009" 1.00
2226799 "721001" "0G0121001" 1.00
2226799 "730001" "0G0230001" 1.00
2226799 "855010" "0E0750010" 1.00
2226799 "815005" "0E0115005" 1.00
2226799 "140009" "0B0840009" 1.00
2226799 "721001" "0G0121001" 1.00
2226799 "730001" "0G0230001" 1.00


I need to put everything on one line where $1 matches like this.

e.g.2226697 29/01/08 2226697 "630511" "0P0705011" 1.00

any help would be grate. Thanks in advance

 
Hi

I would do it with [tt]awk[/tt]. One possible way :
Code:
awk -vORS= '{print l==$1?" ":"\n";l=$1}1;END{print""}' /input/file > /output/file
Tested with [tt]gawk[/tt].

Feherke.
 
Hi thanks for this Feherke. I have put the following in a test script.

awk -vORS ='{print l==$1?" ":"\n";l=$1}1;END{print""}' inputpc.txt > pcompmw.txt

I am getting the following error.

# ./test.sh
awk: syntax error near line 1
awk: bailing out near line 1

Also, sorry didn't tell you I am running on solaris 9, unix, ksh.





 
solaris
Code:
nawk 'BEGIN{ORS=""}
{print (l==$1)?" ":"\n";l=$1;print}
END{print "\n"}
' inputpc.txt > pcompmw.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you very much that worked a treat. I used PHV's concept and it worked a treat. Many thanks again to both Feherke and PHV for your kind help.
 
FYI feherke, I don't think standard awks support expr ? trueval : falseval type logic.

Annihilannic.
 
Hi

As far as I know, the [tt]?:[/tt] conditional operator is a standard feature. There is a GNU extension indeed. But it only permits to wrap the expression for readability after the [tt]?[/tt] and [tt]:[/tt] characters :
Code:
[b]BEGIN[/b] {
  [b]print[/b] 1?
        2:
        3
}
While in standard [tt]awk[/tt] ( or [tt]gawk[/tt] with --posix switch ) you have to write it like :
Code:
[b]BEGIN[/b] {
  [b]print[/b] 1? [red]\[/red]
        2: [red]\[/red]
        3  
}
Please let me know if you ( or anyone else ) know about other difference in the [tt]?:[/tt] functionality. Thanks.

I am wondering why this extension... Maybe to permit easier commenting ?
Code:
[b]BEGIN[/b] {
  [b]print[/b] 1? [gray]# comment[/gray]
        2: [gray]# comment[/gray]
        3  [gray]# comment[/gray]
}

Feherke.
 
You're absolutely correct; I guess I just never tried it! Probably because I dislike the syntax; I don't find it very readable.

Thanks for the correction.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top