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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending information to the end of a line

Status
Not open for further replies.

websolaris

Technical User
Apr 9, 2002
4
US
Hi all,

Newbie to the programming world and would like some help on something. Can this be done in awk or would I use some other language.

I have 2 files fileA and fileB

fileA has data:

B5432CB: file1234.us
B5643CB: file1234.bk

fileB has data:

file1234.us:::uka:ttk:
file1234.bk:::cbk:kkt:

Output file would look like this:

file1234.us:::uka:ttk:B5432CB:
file1234.bk:::cbk:kkt:B5643CB:


1. I don't want B5432CB: to be appended to same line everytime I run this.
2. The B5432CB: will be populated into field 6 everytime I run this.

Any suggestions will be grateful. This is what I have so far:

for row in `cat fileB`
do
filename=`echo $row |cut -d: -f1`
jcl=`grep -i $filename fileA`
jcljobname=`echo $jcl | awk '{print $1}'`
echo "$row$jcljobname" >> prod
done

This only works if I run the script with original fileB with no appends from previous runs.

Thank you.
 
Working with multiple files in awk will produce the
same kind of headaches as the shell.

Your description of the data formatting is lacking.
You are leaving us with a lot to be extrapolated.

I'm assuming that:
1) Fld2 from fileA has to match the first triple
colon delineated segment of fileB?


The easiest way is just to load fileA into an array in
the BEGIN section and then append the information to
matching lines in fileB.

There are plenty of examples of this in the forum
if you look through the posts.

 
The "easiest way" (as outlined by marsd) is...

awk 'BEGIN {while(getline<&quot;fileA&quot;) a[$2]=$1; FS=&quot;:&quot;} {print $0 a[$1]}' fileB

Tested...
file1234.us:::uka:ttk:B5432CB:
file1234.bk:::cbk:kkt:B5643CB:
 
Marsd/Ygor,

Thanks for the reply.

Marsd, you are correct on field 2 fileA must match first triple colon delineated segment of fileB.

Ygor, your awk works but if I use the output file as the the new fileB and use same fileA, I get dups. For example:

file1234.us:::uka:ttk:B5432CB:B5432CB:
file1234.bk:::cbk:kkt:B5643CB:B5643CB:

How can I go about not to inserts dups. Because fileA will be generated once a month and it will contain new data. And I want to update fileB with this data without dups.

Thanks for you input.

websolaris
 
Try something like this:
Code:
awk 'BEGIN {while(getline<&quot;fileA&quot;) a[$2]=$1; FS=&quot;:&quot;}
length($6)==0 {print $0 a[$1];next}
{print}
' fileB


Hope This Help
PH.
 
awk 'BEGIN {while(getline<&quot;fileA&quot;) a[$2]=$1; OFS=FS=&quot;:&quot;} {print $1,$2,$3,$4,$5,a[$1]}' fileB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top