Announcement

Collapse
No announcement yet.

[SOLVED] sed, or better awk - find text and append it to end of line

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [SOLVED] sed, or better awk - find text and append it to end of line

    OK, so I have a file that's a list of filenames. Each of these has a single numeric value in it, easily matched by
    Code:
    [0-9]+[.][0-9]+
    I then want to append this to the end of the line, with a space seperator. How do I do this?
    I am running Ubuntu 8.10 (yes Gnome) with upgrades applied daily about 0900 UK time. Hardware is Dell Precision 420, 2x 800 MHz PIII, 512 MB RDRAM, nVidia GeForce 6800 128 MB AGP graphics, 18GB SCSI and 500GB IDE HDDs, DVD burner, Hauppage TV card.

    #2
    Re: sed (or otherwise) - find text and append it to end of line

    Without knowing the format of the lines in your file I can't tell whether there is a simpler way, but
    Code:
    awk 'match($0,/[0-9]+[.][0-9]+/) {print $0, substr($0,RSTART,RLENGTH)}' inputfile_name > editedfile_name
    should do it

    Comment


      #3
      Re: sed (or otherwise) - find text and append it to end of line

      Thanks. I spoke to IRL friends and one suggested awk as a better tool for the job. Mine is a bit different, mainly because I wasn't sure if I could put the substr command in the print statement, and also the requirements changed to comma seperated.

      Code:
      /[0-9][0-9]*[.][0-9][0-9]*/ {match($1, /[0-9][0-9]*[.][0-9][0-9]*/)
      temperature = substr($1, RSTART, RLENGTH)
      print $1","temperature",""c"
      }
      I am running Ubuntu 8.10 (yes Gnome) with upgrades applied daily about 0900 UK time. Hardware is Dell Precision 420, 2x 800 MHz PIII, 512 MB RDRAM, nVidia GeForce 6800 128 MB AGP graphics, 18GB SCSI and 500GB IDE HDDs, DVD burner, Hauppage TV card.

      Comment

      Working...
      X