sed conditional join of lines

November 9, 2010 at 02:11:24
Specs: HP-UX B.11.11 U
Would appreciate some help again please


My file looks like:

hed, hed2, hed3, hed4, hed5, hed6

1,fiea1 1, fiea1 2, fiea1 3, fiea1 4, last fiea1
1,fieb1 1, fieb1 2, fieb1 3, fieb1 2, last fieb1
2,fieb2 1, fieb2 2, fieb2 3, fieb2 4, last fieb2
1,fiec1 1, fiec1 2, fiec1 3, fiec1 4, last fiec1
2,fiec2 1, fiec2 2, fiec2 3, fiec2 4, last fiec2
1,fiedd 1, fiedd 2, fiedd 3, fiedd 4, last fiedd
1,fiee1 1, fiee1 2, fiee1 3, fiee1 4, last fiee1
2,fiee2 1, fiee2 2, fiee2 3, fiee2 4, last fiee2

Is there a way please of joining the second line conditionally?
I would like to see:

1,fiea1 1, fiea1 2, fiea1 3, fiea1 4, last fiea1
1,fieb1 1, fieb1 2, fieb1 3, fieb1 2, last fieb1 last fieb2
1,fiec1 1, fiec1 2, fiec1 3, fiec1 4, last fiec1 last fiec2
1,fiedd 1, fiedd 2, fiedd 3, fiedd 4, last fiedd
1,fiee1 1, fiee1 2, fiee1 3, fiee1 4, last fiee1 last fiee2

The file is not fixed length but is delimited by commas


Not fussy on how this is achieved - any solution will be grateful: sed, awk, perl, cut of line2 and a join

Will continue seeking a solution myself

Many thanks in anticipation.


See More: sed conditional join of lines

Report •

#1
November 9, 2010 at 09:45:32
I will get you started. This awk program sets the field seperator to comma and skips the first line:

awk ' BEGIN { FS="," }
{
# skip the first line
if(NR == 1)
   continue

# this below would print the entire line and a carriage return
#   printf("%s\n", $0)

# line below prints field 1 with no CR
printf("%s", $1)
} ' datafile

Now, all you have to do is determine what to do if field 1 equals == a 1 or a 2.


Report •

#2
November 9, 2010 at 09:48:57

That's great, thanks, nails.

I will give it a go as I need to be able to think around the problem for myself. Hope to come up with something that will work.

C


Report •
Related Solutions


Ask Question