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!

How do i do this with awk.... 3

Status
Not open for further replies.

buccaneers

Programmer
Jun 3, 2004
28
US
Hello Gurus,

I have a question rather i am stuck at this point.

I have two files. One containing a single record

Jul 8 2004 6:03:00:000PM ~~123456789 ~~13571

And other containing multiple records

~~10001A ~~N ~~123450.0 ~~0.0
~~10002B ~~N ~~234560.0 ~~0.0
~~10003A ~~N ~~345670.0 ~~0.0
~~10004A ~~N ~~456780.0 ~~0.0

What i want to see in output is
merging of these to files in such a way that
Jul 8 2004 6:03:00:000PM ~~123456789 ~~13571

is repeated in all the lines at the start of each line.

Output Example :-
Jul 8 2004 6:03:00:000PM ~~123456789 ~~13571 ~~10001A ~~N ~~123450.0 ~~0.0
Jul 8 2004 6:03:00:000PM ~~123456789 ~~13571 ~~10002B ~~N ~~234560.0 ~~0.0

How can i do this using awk ?

TIA.
 
This is untested but may get you started

awk -f buccaneers.awk file1 file2

# buccaneers.awk
NR==1{a=$0;next}
{print a, $0}

CaKiwi
 
prefix.txt:
Jul 8 2004 6:03:00:000PM ~~123456789 ~~13571


suffix.txt:
~~10001A ~~N ~~123450.0 ~~0.0
~~10002B ~~N ~~234560.0 ~~0.0
~~10003A ~~N ~~345670.0 ~~0.0
~~10004A ~~N ~~456780.0 ~~0.0



merging prefix.txt and suffix.txt:
Code:
awk 'FILENAME == "prefix.txt" { pref = $0 } FILENAME == "suffix.txt" { print pref, $0 }' prefix.txt suffix.txt
 
CaKiwi,

It isn't working.. i am getting error
awk: syntax error near line 1
awk: bailing out near line 1

I just copied your solun into a file adn exec'ed file.

I understand you are checking for number of records to 1 from first file and assigning the first record to "a". Then you are trying to print the variable "a" and the current line from file2.

I hope this is what you are trying to do. Can you explain what exactly are you trying to do out here ?

TIA.
 

NR==1{a=$0;next}

For the first record read (record in file 1), save whole line in variable a and and go to read next record

{print a, $0}

for all other records, (all records of file 2) print out the saved data followed by the current record.

What system and version of awk are you using? Exactly how are you running it? If you are on Solaris, use nawk.

(Hi Krunek, welcome back)

CaKiwi
 
Something like this ?
awk '
BEGIN{getline p<"/path/to/firstfile"}
{print p,$0}
' /path/to/secondfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thank you for greetings, CaKiwi! God bless you.

I've tried your solution and it works perfect:
awk 'NR == 1 { a = $0; next } { print a, $0 }' prefix.txt suffix.txt

Bye!

KP.
 
Thx CaKiwi. I am using solaris. let me try with nawk.

Thx Krunek this solution works.

Thx PHV the solution works.

Really appreciate all of your help.

Have a gr8 weekend.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top