Computing.Net > Forums > Unix > combining fields with awk.

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

combining fields with awk.

Reply to Message Icon

Original Message
Name: Natalie
Date: March 1, 2004 at 14:05:19 Pacific
Subject: combining fields with awk.
OS: unix on osx
CPU/Ram: g3 256mg ram
Comment:

The input file has

dslkfj , adkf , Primrose , 1
lksdfj , Begonia , 1
ldskfjkd , lsdkfdk , Yellow Rose , 2

The first fields containing prose need to be combined into
one, and the last two fields remain separate so the file will
contain

dslkfj adkf , Primrose , 332
lksdfj , Begonia , 235
ldskfjkd lsdkfdk lkdsfk , Yellow Rose , 463

Can I use a -$1 for the fields to combine them and how?
Many Thanks,
Natalie


Report Offensive Message For Removal


Response Number 1
Name: thepubba
Date: March 1, 2004 at 16:01:41 Pacific
Reply: (edit)

First off, your data file has 4 fields in the first entry, 3 on the second and 4 on the third. Your output file example shows 4 fields on the first line, 3 on the second and 5 on the third. Additionally, the numbers in the output file are not the same as those on the input file (the last field). This is easy enough to do, and there are an abundance of good scripters who will help you. You just need to post a better example. How do you arrive at the different number on your output file? You said combine the first 2 fields and leave the second 2 comma separated. That would indicate you expect 4 fields in each entry. Why does the second only have 3?


Report Offensive Follow Up For Removal

Response Number 2
Name: Natalie
Date: March 1, 2004 at 22:35:17 Pacific
Reply: (edit)

Let me restate.

I have variying numbers of fields due to my original
output source. The original output source is not a
database program.

The last two fields of each record are consistent and all
preceeding fields need to be combined into one.

In the output I want just 3 Fields. The first field would be
the instructions, the second, the plant name, the third, a
number. What is intended to be the first field
could be spread to 1, 2, or more fields.

Try this input example (The first row has 4 comma
separated fields, the second 3, the third 5)

Pick the , flower , Primrose , 332
plant the seed , Begonia , 235
dig a hole , five feet , deep , Yellow Rose , 463

to
(Output: each row has 3 comma separated fields, working
from the right.)

Pick the flower , Primrose , 332
plant the seed , Begonia , 235
dig a hole five feet deep , Yellow Rose , 463

I want to use the last two fields as an anchor and combine
everything else. Is it possible?
Wait...
I think I could try printing the fields backwards starting
with the maximum field number, $8 , $7..$1
then remove double "," separators at the begining with
sed until text, then pring the new field $1, $2 and
combine all remaining fields.
Thanks for helping me think this out. I'll try this.

Thanks,
Natalie


Report Offensive Follow Up For Removal

Response Number 3
Name: aigles
Date: March 1, 2004 at 22:45:46 Pacific
Reply: (edit)

Try that :


awk '
BEGIN {
   FS = "," ; OFS = ","
}
{
  for (i=2; i<=NF-2; i++)
      $1 = $1 $i; 
  print $1,$(NF-1),$NF;
}
' input_file

Jean-Pierre.


Report Offensive Follow Up For Removal

Response Number 4
Name: Natalie
Date: March 14, 2004 at 11:11:06 Pacific
Reply: (edit)

Thanks, I will try this. I had written something quite
awful.

Many, Many thanks,
Natalie


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge
Poll History




Data Recovery Software