![]() |
|
|
|
|
|||||||
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| script for a 3 line paragraph | invinzin21 | Shell Programming and Scripting | 2 | 12-17-2007 09:11 PM |
| Appending a line in a file after a particular line | maxvirrozeito | Shell Programming and Scripting | 7 | 12-12-2007 09:58 AM |
| Appending line ending with '}" to new line | aismann | Shell Programming and Scripting | 4 | 08-12-2007 11:09 PM |
| Appending the line number and a seperator to each line of a file ? | pjcwhite | Shell Programming and Scripting | 4 | 03-20-2007 09:29 PM |
| Convert a paragraph to single line | rimss | Shell Programming and Scripting | 6 | 06-07-2006 12:17 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
Code:
awk ' BEGIN{FS="= "}
/Condition/ {cond = $2 ; print $0 ;next}
/^$/ { print cond ; next}
{print $0 " " cond}
' "file"
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 |
|||
| Google UNIX.COM |
| Thread Tools | |
| Display Modes | |
|
|