SED or AWK: Appending a line Identifier that changes with paragraph?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers SED or AWK: Appending a line Identifier that changes with paragraph?
# 1  
Old 04-27-2007
Question SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day.

I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file.

I.e.


----Begin Original Datafile-----
Condition = XXX

Header Line 1
Header Line 2

Station Data
1 5.43
2 6.43
3 7.8
4 450
5 650

Condition = YYY

Header Line 1
Header Line 2

Station Data
1 654
2 987
3 875
4 874
5 678

Condition= ZZZ
ZZZ
.
.
.
.
----------End Data File-----------


and I would like to convert it to


--------Begin Desired Datafile ------
Condition = XXX
XXX
Header Line 1 XXX
Header Line 2 XXX
XXX
Station Data XXX
1 5.43 XXX
2 6.43 XXX
3 7.80 XXX
4 4500 XXX
5 6506 XXX
XXX
Condition = YYY
YYY
Header Line 1 YYY
Header Line 2 YYY
YYY
Station Data YYY
1 654 YYY
2 987 YYY
3 875 YYY
4 874 YYY
5 678 YYY
YYY
Condition= ZZZ
ZZZ
.
.
.
------End Desired Datafile ------

With possibly several thousand unique Condition ID's and datasets. There are also some blank and header lines before each data set and appending the line identifier to those is just fine. All the blank and header lines are already being deleted at a later step.

If it were just a few cases I could hard code it, but having the condition ID update as it goes is beyond me at the moment.

Thanks in advance! So far you all have been extremely helpful and I'll definitely be back.
Cheers
Josh
# 2  
Old 04-28-2007
Code:
awk ' BEGIN{FS="= "} 
      /Condition/ {cond = $2 ; print $0    ;next}
      /^$/ { print cond ; next}
      {print $0 " " cond}
    ' "file"

output:
Code:
# ./test.sh
Condition = XXX
XXX
Header Line 1 XXX
Header Line 2 XXX
XXX
Station Data XXX
1 5.43 XXX
2 6.43 XXX
3 7.8 XXX
4 450 XXX
5 650 XXX
XXX
Condition = YYY
YYY
Header Line 1 YYY
Header Line 2 YYY
YYY
Station Data YYY
1 654 YYY
2 987 YYY
3 875 YYY
4 874 YYY
5 678 YYY
YYY
Condition= ZZZ
ZZZ ZZZ

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed for appending a line before a pattern and after another patter.

Hi All, I'm appending a line before a pattern and after another pattern(;) but omitting the first pattern - same as if comes duplicates before the second pattern. Also, I'm adding a word before the first pattern - Here is my file select blah blah hello select blah blah ; select... (6 Replies)
Discussion started by: Mannu2525
6 Replies

2. Shell Programming and Scripting

Reading and appending a row from file1 to file2 using awk or sed

Hi, I wanted to add each row of file2.txt to entire length of file1.txt given the sample data below and save it as new file. Any idea how to efficiently do it. Thank you for any help. input file file1.txt file2.txt 140 30 200006 141 32 140 32 200006 142 33 140 35 200006 142... (5 Replies)
Discussion started by: ida1215
5 Replies

3. UNIX for Dummies Questions & Answers

Appending columns at the end of output using awk/sed

Hi , I have the below ouput, =====gopi===== assasassaa adsadsadsdsada asdsadsadasdsa sadasdsadsd =====kannan=== asdasdasd sadasddsaasd adasdd =====hbk=== asasasssa .... .. I want the output like as below, not able paste here correctly. (2 Replies)
Discussion started by: eeegopikannan
2 Replies

4. Shell Programming and Scripting

Awk/Sed - appending within file

hello all, First time post here. I have searched a bit but could not find an exact answer. I have about a week's experience of Sed and Awk, and am having fun, but am a little stuck. I am reformatting an xml file into json format. I have got this far: {"clients": ...and so on. What I want... (22 Replies)
Discussion started by: singerfc
22 Replies

5. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

6. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

7. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

8. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies

9. Shell Programming and Scripting

Appending line with sed works on Linux but not on Solaris

Hi folks, Our application installation uses "sed" command to append string after specific line or after line number. Both cases work perfect on Linux but fail on Solaris. The OS versions are Solaris 9 and Linux Red Hat AS 3. i.g: Linux: ----- file foo.txt aaa bbb ccc ddd root#... (4 Replies)
Discussion started by: nir_s
4 Replies

10. Shell Programming and Scripting

Appending line/lines with sed

Hi folks, I need to append line or bulk of lines into a file. For example,I have the following section in opmn.xml file: <process-type id="OC4J_RTEadmin_NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data... (28 Replies)
Discussion started by: nir_s
28 Replies
Login or Register to Ask a Question