Find the answer to your Linux question:
Results 1 to 4 of 4
Hi everyone I need to use sed or awk to append text to lines in which a pattern has been matched. In other words, add the character '5' to every ...
  1. #1
    jls
    jls is offline
    Just Joined!
    Join Date
    Apr 2007
    Posts
    13

    Awk/sed to add text

    Hi everyone

    I need to use sed or awk to append text to lines in which a pattern has been matched.

    In other words, add the character '5' to every line containg the string 'BLAH'

    I find the man pages very cryptic and don't have time at the moment to lear these applications. PLease help.

    Thanks

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    example input:
    Code:
    aaaaa
    sadafsa
    blah asljfsldf
    asfdsad
    asfdsfad
    asfdfsafd
    blah xxxxx
    asdfsafdsa
    asfdasdfa
    sadfasdsaf
    output:
    Code:
    # awk '/blah/{ $0=$0"5" } {print} ' file
    aaaaa
    sadafsa
    blah asljfsldf5
    asfdsad
    asfdsfad
    asfdfsafd
    blah xxxxx5
    asdfsafdsa
    asfdasdfa
    sadfasdsaf

  3. #3
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    Let's take an example: Suppose a1.txt contains the lines..see cat command output on a1.txt file.

    $ cat a1.txt
    BLAH arun
    arun arun
    arun BLAH
    Jhola Linux BLAH Hurray!
    Quiting after entring the line below
    Bye ByeBLAH!!!


    # this will give change BLAH to BLAH5

    $ sed -n '/BLAH/s/BLAH/&5/ p' a1.txt
    BLAH5 arun
    arun BLAH5
    Jhola Linux BLAH5 Hurray!
    Bye ByeBLAH5!!!

    #Same as above, but useful if a line contains multiple BLAH keyword

    $ sed -n '/BLAH/s/BLAH/&5/gp' a1.txt
    BLAH5 arun
    arun BLAH5
    Jhola Linux BLAH5 Hurray!
    Bye BLAH5 ByeBLAH5!!!


    # This will append your desired text lets say "5" at the last of the line which # contains BLAH keyword

    $ sed -n '/BLAH/s/$/5/ p' a1.txt
    BLAH arun5
    arun BLAH5
    Jhola Linux BLAH Hurray!5
    Bye ByeBLAH!!!5

    I hope this will help...or let us know.

    Cheers!
    /Arun Sangal
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  4. #4
    jls
    jls is offline
    Just Joined!
    Join Date
    Apr 2007
    Posts
    13


    Thanks guys for the quick response and the help. Sed is now my friend!

Posting Permissions

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