Find the answer to your Linux question:
Results 1 to 4 of 4
I've got the same problem. I'd like to read a file line by line to substitute a pattern. The line gets splitted when the shell finds a space. I'd like ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    1

    read a file line by line to substitute a pattern

    I've got the same problem. I'd like to read a file line by line to substitute a pattern. The line gets splitted when the shell finds a space.

    I'd like to do something like:

    imagine the line "MP3Random On" inside a file called mp3_mod.conf
    I'd like a script to be able to replace que word On for Off (or vice-versa)

    for line in $(cat file); do
    ... do some grep test ...
    done

    Can anyone give me some help ?

    Thank you very much.


    Quote Originally Posted by anomie
    Ok, got it figured out. I just needed to expand my toolset a bit.

    One way for reading line by line:
    Code:
    awk '/./' some-text-file

  2. #2
    Linux Enthusiast
    Join Date
    Jul 2005
    Location
    Maryland
    Posts
    521
    I'm not a programmer, but using anomie's "awk" example, I think you can do this:
    Code:
    cat file
    MP3Random1 On
    MP3Random2 Off
    MP3Random3 On
    MP3Random4 Off
    MP3Random5 On
    Code:
    awk '/./' file | sed 's/On/Off/'
    will give you:
    Code:
    MP3Random1 Off
    MP3Random2 Off
    MP3Random3 Off
    MP3Random4 Off
    MP3Random5 Off

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    simplest case is like this
    Code:
    sed 's/On$/Off/' file

  4. #4
    Linux User
    Join Date
    Aug 2005
    Posts
    408
    A little more specifically, sed can find that specific line and replace all instances of on with off for it.

    for instance, the following
    Code:
    sed -e 's/\(MP3Random\) On/\1 Off/g' sample
    seeks out and captures the "MP3Random" bit and then just reprints what was captured with "\1" and then prints something new after it.

Posting Permissions

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