The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script for a 3 line paragraph invinzin21 Shell Programming and Scripting 2 12-18-2007 01:11 AM
Appending a line in a file after a particular line maxvirrozeito Shell Programming and Scripting 7 12-12-2007 01:58 PM
Appending line ending with '}" to new line aismann Shell Programming and Scripting 4 08-13-2007 02:09 AM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-21-2007 01:29 AM
Convert a paragraph to single line rimss Shell Programming and Scripting 6 06-07-2006 03:17 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-27-2007
selkirk selkirk is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 4
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 (permalink)  
Old 04-28-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0