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 ...
- 04-04-2007 #1Just 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.
Originally Posted by anomie
- 04-04-2007 #2Linux 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
will give you:Code:awk '/./' file | sed 's/On/Off/'
Code:MP3Random1 Off MP3Random2 Off MP3Random3 Off MP3Random4 Off MP3Random5 Off
- 04-04-2007 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
simplest case is like this
Code:sed 's/On$/Off/' file
- 04-04-2007 #4Linux 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
seeks out and captures the "MP3Random" bit and then just reprints what was captured with "\1" and then prints something new after it.Code:sed -e 's/\(MP3Random\) On/\1 Off/g' sample


Reply With Quote