Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Append a line of text to the end of the previous line

  • 11-12-2008 06:05PM
    #1
    Registered Users, Registered Users 2 Posts: 37


    I need to append a line of text to the end of the previous line. My data is as follows:

    dev hostname user user01 show
    * deviceid:6d41fc
    OK
    dev hostname user user02 show
    * deviceid:d8c18e
    OK
    dev hostname user user03 show
    * deviceid:abd00d
    OK

    I need it in the following format and there are a few thousand lines of it:

    dev hostname user user01 show * deviceid:6d41fc
    OK
    dev hostname user user02 show * deviceid:d8c18e
    OK
    dev hostname user user03 show * deviceid:abd00d
    OK

    or even better:

    dev hostname user user01 show * deviceid:6d41fc
    dev hostname user user02 show * deviceid:d8c18e
    dev hostname user user03 show * deviceid:abd00d

    Can anyone help with this?


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 3,519 ✭✭✭ethernet


    I suppose you could use this data as a source file and write a script to pull bits from it, perhaps using cut and specifying a space as the delimiter before dumping to another file (or standard output). Don't know of any fancy shortcuts for this but one of the gurus will probably have one.


  • Registered Users, Registered Users 2 Posts: 23,202 ✭✭✭✭Tom Dunne


    I don't follow what you are trying to do.

    Are you Reading from one file, outputting to another?


  • Registered Users, Registered Users 2 Posts: 37 market_garden


    Hi Tom,
    I have a text file containing the following:

    dev hostname user user01 show
    * deviceid:6d41fc
    OK
    dev hostname user user02 show
    * deviceid:d8c18e
    OK
    dev hostname user user03 show
    * deviceid:abd00d
    OK
    .
    . and so on.

    I want to either rearrange the text in the file or read the file and output to another so long as I can get the information in the following format:

    dev hostname user user01 show * deviceid:6d41fc
    dev hostname user user02 show * deviceid:d8c18e
    dev hostname user user03 show * deviceid:abd00d


  • Registered Users, Registered Users 2 Posts: 23,202 ✭✭✭✭Tom Dunne


    I see.

    The best approach would be to read in the source file, do the search, and write it out to a second file.

    So, you would have a loop that does the following:

    look for a string, starting with "dev" and ending with "show" -> first piece
    then look for a string "*" -> second piece
    then search for "deviceid" and continue on for seven characters (to catch the : and the hex number) -> third piece

    I assume you don't want the "OK", so you can ignore that.

    Then you combine the three pieces from above, and write them out to a file as one line.

    Perl would be your man here, regular expressions are what you are looking for. :)


  • Registered Users, Registered Users 2 Posts: 1,822 ✭✭✭EvilMonkey


    cut or awk could also be what your loking for. look at the man page


  • Advertisement
  • Closed Accounts Posts: 12 Aquafresh


    The following should work:
    cat data | gawk 'BEGIN{ORS=""} {if(NR % 3 == 1) print; else if(NR % 3 == 2) print " "$0; else print "\n"}'
    

    i.e. print the first line, a space, the second line, a new line and so on for all lines mod 3. You need the ORS="" because the default output record seperator is "\n" and you don't want newlines between the first and second record etc.


Advertisement