Operating System - HP-UX
1833873 Members
2081 Online
110063 Solutions
New Discussion

Replace two words in a file

 
SOLVED
Go to solution

Replace two words in a file


Hi guys, this should be nice and easy :)

I'm not the best at scripting and can't make sense of the man pages. I need to change line two of a 100+ files in a directory.

line two currently reads: GROUP "Imp mode"

I want to change this to: GROUP "test mode"

pseudo:

for each file in directory do
replace line two field two with "test mode" (including quotes)
done.

I started reading man pages for awk but then gave up very quickly. Started reading through sed but then thought of this forum....

Would appreciate any help.

Thanks.

Regards

Mohammed U. Ali.
Life is what you make it!
15 REPLIES 15
G. Vrijhoeven
Honored Contributor

Re: Replace two words in a file

Mohammed

for i in `find / -type f`
do
file $i | grep -q text
if [ $? -eq 0 ];then
cat $i | sed s'/imp mode/test mode/'>$i.out
mv $i.out $i
fi
done



Gidoen
Klaus Crusius
Trusted Contributor

Re: Replace two words in a file

Mohammed

the same with awk:

for i in `find / -type f`
do
file $i | grep -q text
if [ $? -eq 0 ];then
awk <$i '
NR == 2 { sub("imp mode", "test mode" }
{ print }' >$i.out
mv $i.out $i
fi
done



Gidoen
There is a live before death!

Re: Replace two words in a file

Thanks guys,

Fortunately I can make sense of the syntax :)

I'm still trying to understand why you used '-type f' in your find syntax... what does f stand for? (file?)

I've found that using 'find .' would be enough?

Kind regards

Mohammed.
Life is what you make it!
G. Vrijhoeven
Honored Contributor

Re: Replace two words in a file

Mohammed,

find . ( or / = location) -type f (= regular file)

file $i = to find out if the file is text or binary.

Gideon

Re: Replace two words in a file

Thanks,

I've just realised that not all the files have line GROUP "Imp mode" some have GROUP "something else"

Could I just script it so that it replaces whatever is in the quotes?

Regards

Mohammed.
Life is what you make it!
G. Vrijhoeven
Honored Contributor

Re: Replace two words in a file

Mohammed:

for i in `find / -type f`
do
file $i | grep -q text
if [ $? -eq 0 ];then
cat $i | sed s'/"...................................."/"test mode"/'>$i.out
mv $i.out $i
fi
done

a . = any character.

Gideon



Re: Replace two words in a file


Hi,

The problem with that is that I have other lines in the file that have text in quotes. So it will affect them also.

Could I not be specific i.e. specify line 2 field 2 or the word after GROUP?

Regards

Mohammed.
Life is what you make it!
H.Merijn Brand (procura
Honored Contributor

Re: Replace two words in a file

using GNU grep and perl (with in-file replace):

# grep -r 'Imp mode' . | xargs perl -pi -e's/\bImp mode\b/test mode/g'
Enjoy, Have FUN! H.Merijn
G. Vrijhoeven
Honored Contributor

Re: Replace two words in a file

Mohammed:

for i in `find / -type f`
do
file $i | grep -q text
if [ $? -eq 0 ];then
cat $i | sed s'/GROUP "...................................."/GROUP "test mode"/'>$i.out
mv $i.out $i
fi
done

a . = any character.

Gideon



John Palmer
Honored Contributor
Solution

Re: Replace two words in a file

Here's a basic script to edit each file in the current directory 'in situ' with ex...

#!/usr/bin/sh

find . -type f | {
while read F
do
ex $F << EOD
/^GROUP "
s/".*/"test mode"
wq
EOD

done
}

It changes the first record starting 'GROUP "' to 'GROUP "test mode".

Regards,
John/Jimmy
Jean-Louis Phelix
Honored Contributor

Re: Replace two words in a file

Hi,

Much more simplier ...

for i in $(find / -type f -exec grep -q "GROUP" {} \; )
do
awk '{
if ($2 == "GROUP")
print "GROUP \"test mode\""
else
print' $i > $i.out
mv $i.out $i
done

or with sed :

for i in $(find / -type f -exec grep -q "GROUP" {} \; )
do
sed 's/^GROUP ".*"$/GROUP "test mode"/' $i > $i.out
mv $i.out $i
done

Regards
It works for me (© Bill McNAMARA ...)
Adam J Markiewicz
Trusted Contributor

Re: Replace two words in a file

Hi.

I think that
sed s/GROUP *\"[^\"]*\"/GROUP \"test mode\"/
would be much more precise and accurate.

PS.: Looks complicated, but:
* = repeat the previous expression many times (includeing zero times)
[^x] = character that is not 'x'
quotes should be following backslash to ommit their special meaning.

In previous exaples you change every string in quotes.
In this you change only strings in quotes that are following 'GROUP' (and any number of spaces).

Good luck

Adam
I do everything perfectly, except from my mistakes
Jean-Louis Phelix
Honored Contributor

Re: Replace two words in a file

Hi,

I am very sorry ... When I reread this post it's possible to read that my solutions are simplier than others ... It's really not the case. I just wanted to write that the second form of the problem was simplier than the first form ... But with posting delays ...

Regards.
It works for me (© Bill McNAMARA ...)
H.Merijn Brand (procura
Honored Contributor

Re: Replace two words in a file

Mine is of course the shortest and simplest ... but wrong :( [ forgot the -l ]

# grep -r -l 'Imp mode' . | xargs perl -pi -e's/\bImp mode\b/test mode/g'
Enjoy, Have FUN! H.Merijn

Re: Replace two words in a file

Thanks for the overwhelming response guys.

I went with Jean-Louis's sed solution in the end because it was easiest for me to understand. However, I've made note of the other methods and have given you the points you deserve :)

Kind regards,

Mohammed.
Life is what you make it!