Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
The input file has
dslkfj , adkf , Primrose , 1
lksdfj , Begonia , 1
ldskfjkd , lsdkfdk , Yellow Rose , 2The first fields containing prose need to be combined into
one, and the last two fields remain separate so the file will
containdslkfj adkf , Primrose , 332
lksdfj , Begonia , 235
ldskfjkd lsdkfdk lkdsfk , Yellow Rose , 463Can I use a -$1 for the fields to combine them and how?
Many Thanks,
Natalie

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?

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 , 463to
(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 , 463I 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

Try that :
awk '
BEGIN {
FS = "," ; OFS = ","
}
{
for (i=2; i<=NF-2; i++)
$1 = $1 $i;
print $1,$(NF-1),$NF;
}
' input_fileJean-Pierre.

![]() |
![]() |
![]() |

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