Find the answer to your Linux question:
Results 1 to 3 of 3
Hi all, I am trying to do some one-liner preprocessing on a file with sed. I have two cases like so... Case 1: !PRETAG_ Do x to y Case 2: ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5

    sed help -- merging two lines

    Hi all,

    I am trying to do some one-liner preprocessing on a file with sed. I have two cases like so...

    Case 1:
    !PRETAG_ Do x to y

    Case 2:
    !PRETAG> -- WARNING< DONT REMOVE THIS!
    Do x to y

    Basically I need to go from case 1 to 2 and back again. I can easily get from 1 to 2, but then re-merging the lines is giving me trouble. My idea was to replace the first line of case 2(including the newline character) with "!PRETAG_ ", thus pulling the next line up to the end of the edited line. However, SED will not pattern match the following: "s/\!PRETAG> -- WARNING< DONT REMOVE THIS!\n/\!PRETAG_ /"

    If I hex edit the file, I can clearly see that x0A follows each line, so it seems that sed should match it. I have also attempted to use the "N" feature to no avail.

    Any suggestions? I'm pulling out my hair!

    Thanks!

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Should be something like:

    Code:
    sed '
    /!PRETAG> -- WARNING/ {
    	N
    	s/.*\n/!PRETAG_/
    }' file
    Regards

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    5
    Franklin you are correct. The problem that I was experiencing was due to the behavior of the "N" command, I was simply using N on every line, which actually turned out to be every OTHER line, thus my target line was actually being combined with the line BEFORE it instead of AFTER it.

    Adding the conditional that N only acts when the TAG is seen fixed my woes! Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...