Append lines with SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append lines with SED
# 1  
Old 11-05-2008
Append lines with SED

I have the following data in a file and would like to append the lines inside each section 10, 20, 30, 40, etc... onto one line for each of the sections. Is it possible with SED to make this happen??


10 00039393 DOCK: RECEIVE PART, TAG. D D
0000 SHIPPING/RECEIVING I 682 CRT

DOCK: RECEIVE PART. TAG PART.


20 00050670 INSP: VIS/DIM INSP D D OPSK 1
0020 INSPECTION-DIMENSIONAL I 671 RSN 251

INSP: VISUAL/DIMENSIONAL INSPECTION.


30 00039395 GRIND: INSP & RECORD RUNOU D D OPSK 1
2001 DEVLIEG - JIG-MILL I 672 RSN 251
PRSH 1,2,3,4
GRIND: INSPECT AND RECORD RUNOUTS.
MACHINE CHECK TO SAVE PRIOR TO
GRINDING DIAMETERS C AND D.


40 00046094 REVIEW: PL REVIEW FOR REPA D D
0055 PRODUCTION I 677

REVIEW: PRODUCT LEADER REVIEW FOR
REPAIRS.
# 2  
Old 11-05-2008

I'd recommend awk rather than sed.

# 3  
Old 11-12-2008
awk command

I'm not familiar with awk (not that familiar with sed either). Any thoughts on what my command would look like??
# 4  
Old 11-12-2008
If you have GNU awk:

Code:
awk --re-interval 'END { 
  if (r) print r, RS 
  }
/^[0-9]* [0-9]{8}/ && r {
  print r, RS; r = "" 
  }
{ r = r ? r FS $0 : $0 }
' infile

If you're on Solaris, you should use /usr/xpg4/bin/awk:

Code:
/usr/xpg4/bin/awk 'END { 
  if (r) print r, RS 
  }
/^[0-9]* [0-9]{8}/ && r {
  print r, RS; r = "" 
  }
{ r = r ? r FS $0 : $0 }
' infile

If you're on other platorm and your awk implementation does not support re-interval, you should modify the regular expression.

Other possible solutions with GNU awk:

Code:
awk '$1=$1' RS='\n\n\n' ORS='\n\n'  infile

Or (if you don't wont to squeeze the blank characters):

Code:
awk  -F'\n' '$1=$1' RS='\n\n\n' ORS='\n\n'  infile


Last edited by radoulov; 11-12-2008 at 10:35 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

2. UNIX for Dummies Questions & Answers

append following lines to 1st line, every 3 lines

I have output like this: USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 USER_ID 12/31/69 19:00:00 12/31/69 19:00:00 ... where USER_ID is a unique user login followed by their login timestamp and... (6 Replies)
Discussion started by: MaindotC
6 Replies

3. Shell Programming and Scripting

Append lines for a specific pattern

Input: 09:43:46,538 INFO first text 10:45:46,538 INFO second text 11:00:46,538 INFO third more text Output: 09:43:46,538 INFO first text 10:45:46,538 INFO second text 11:00:46,538 INFO third more text The rule is to append all lines so each line contains this format... (7 Replies)
Discussion started by: chitech
7 Replies

4. UNIX for Dummies Questions & Answers

how do I append new lines to awk variable?

I want to build an array using awk, consisting only of a subset of lines of a file. I know how to have awk assess whether a key phrase is in a particular line, but I can't find anywhere how to then append the line containing that phrase to an array that has previously-found lines also containing... (9 Replies)
Discussion started by: pts2
9 Replies

5. Shell Programming and Scripting

Append lines in a file

Legends, Please help me out. I have a file abc.txt with the following entries JACK JIL SANDY amer europe pak Now, i want to append the character after each line. but, condition is if it is UPPER case then character appended should be uppercase, else lowercase example: JACKL2... (5 Replies)
Discussion started by: sdosanjh
5 Replies

6. Shell Programming and Scripting

Append lines in a file

Hi All, I have a file separated by , and each line end with ".If the line doesnt end with " then i need to join the current line and the next one and put them in another file a, b, c,d" d,f,g,h k, l m" o,p,q,r,t" ouput : a,b,c,d" d,f,g,h,k,l,m" o,p,q,r,t" (2 Replies)
Discussion started by: gwrm
2 Replies

7. Shell Programming and Scripting

Append transaction header lines to same transaction's detail lines

Hi guys, I was wondering if someone can give me a hand in helping me append transaction header line in a file at the end of the transaction detail lines. Basically, I have a file that looks like this: FHEAD File1 THEAD TRANS1-blah TDETL HI1 TDETL HI2 TDETL HI3 TTAIL TRANS1-blah THEAD... (3 Replies)
Discussion started by: rookie12
3 Replies

8. Shell Programming and Scripting

How to append using sed

Hello all, I want to use sed to append a variable stored in $i, how do i do that? $i contains a path to a directory and i want to append it before the result i get from awk statement, before affecting awk results. rite now out put i get from code below is:- The path is:... (2 Replies)
Discussion started by: asirohi
2 Replies

9. Shell Programming and Scripting

Append variable to only certain lines

Hi There! I'm trying to write a shell script which generates a random hexadecimal number and then appends it to the end of lines XX onwards of a certain file. XX is determined by a certain string on the previous line. Thus for example, if the input file is I search for the string... (3 Replies)
Discussion started by: orno
3 Replies

10. UNIX for Dummies Questions & Answers

append word to lines

Hi all, Can someone suggest how to append some word to all lines in file. for example word "Honey" to file f1 with lines: Mia Katrin Elizabeth to get Honey Mia Honey Katrin Honey Elizabeth Thanks in advance Givi (3 Replies)
Discussion started by: giviut
3 Replies
Login or Register to Ask a Question