Find the answer to your Linux question:
Results 1 to 8 of 8
Hi all, I have been working creating part of a script for the past fours days and am so close to getting it finished but I'm stuck on one thing. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    14

    formatting two lines of output into one line


    Hi all, I have been working creating part of a script for the past fours days and am so close to getting it finished but I'm stuck on one thing.

    I have a folder containing a large number of files and I need the values for two strings within each file.
    To begin with I am searching one file and when I have my code right will put it in a for loop to search every file.

    My code so far is:

    Code:
    grep -E "string1|string2" file | sed 's/div\ class\=\"string1\"/\br\ \//g' | sed 's/div\ class\=\"string2\"/\hr\ \//g' | sed s'/<\/div>//g'
    Which pulls out the two strings, does a bit of tidying up and produces the following:

    <br />outputline1
    <hr />outputline2

    I just need these two lines to on one line, so:

    <br />outputline1<hr />outputline2

    I hope someone may be able to help? I've searched a fair bit and seen lots of references to <tr> but none have worked when applied in my case. I look at this and it seems line it should be such a simple fix, I just can't figure it out!

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    477
    something like this seems to work OK

    tr -d "\n" <a

    where 'a' is the name of the file containing your two lines
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    14
    Thanks tpl - do I pipe into that last command though? I tried doing the following

    Code:
    grep -E "string1|string2" file | sed 's/div\ class\=\"string1\"/\br\ \//g' | sed 's/div\ class\=\"string2\"/\hr\ \//g' | sed s'/<\/div>//g'  | tr -d "\n" <file
    and it just pulls in extra text from the file I'm reading, seemingly at random though I'm sure it isn't.
    Last edited by davidov31; 10-31-2012 at 08:42 AM.

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    221
    hi,

    all those pipes are probably useless.
    one sed should be able to do the whole job.

    can you paste a representative sample of input text file?
    Code:
    sed -n '/string[12]/{s/div class="string1" \br //gs/div\ class\=\"string2\"/\hr\ \//g; s/<\/div>//g';s/div\ class\=\"string2\"/\hr\ \//g; s/<\/div>//g}' yourFile
    the same sed command can put all on one line.
    give us input sample.

  5. #5
    Just Joined!
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    14
    Thanks very much for that, it's really appreciated!
    I hope I'm not assuming too much here but the text I'm looking for is contained in just a couple of specific lines in each file so I'm thihking there is not much point in pasting in a copy of what is quite a large file? I'm happy to do this if required however

    Here is an example of the two lines I'm looking at

    <div class="StockCodeCreators">(W) Duane Swierczynski &amp; Various (A) Michael Avon Oeming &amp; Various (CA) Michael Avon Oeming</div>
    <div class="PreviewsHtml">Michael Avon Oeming's twisted antiheroes head to the pages of DHP, as a three-issue arc of The Victories begins! Also in this issue, get shanghaied by the premiere of Geoffrey Thorne and Todd Harris's Journeymen! Plus, the exciting conclusions of Resident Alien, Captain Midnight, Gamma, and White Suits, and all-new X!
    Last edited by davidov31; 11-02-2012 at 05:41 AM.

  6. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    221
    sorry, I can't find a way using sed (I'm not so keen on sed )

    using awk:
    Code:
    awk '/<div class="StockCodeCreators|PreviewsHtml">/{gsub("<[^>]*>",""); printf("%s",$0)}'
    are lines really one after the other.

    you should have given us more than just the lines your looking for.

  7. #7
    Just Joined!
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    14
    I thought it would be helpful to just post a snippet, here is the full file

  8. #8
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    221
    no! no full file yet

Posting Permissions

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