- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replace two words in a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 02:14 AM
11-22-2002 02:14 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 02:23 AM
11-22-2002 02:23 AM
Re: Replace two words in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 02:29 AM
11-22-2002 02:29 AM
Re: Replace two words in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 03:38 AM
11-22-2002 03:38 AM
Re: Replace two words in a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 03:43 AM
11-22-2002 03:43 AM
Re: Replace two words in a file
find . ( or / = location) -type f (= regular file)
file $i = to find out if the file is text or binary.
Gideon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 03:48 AM
11-22-2002 03:48 AM
Re: Replace two words in a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 03:56 AM
11-22-2002 03:56 AM
Re: Replace two words in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:08 AM
11-22-2002 04:08 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:09 AM
11-22-2002 04:09 AM
Re: Replace two words in a file
# grep -r 'Imp mode' . | xargs perl -pi -e's/\bImp mode\b/test mode/g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:11 AM
11-22-2002 04:11 AM
Re: Replace two words in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:14 AM
11-22-2002 04:14 AM
Solution#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:15 AM
11-22-2002 04:15 AM
Re: Replace two words in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:26 AM
11-22-2002 04:26 AM
Re: Replace two words in a file
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:30 AM
11-22-2002 04:30 AM
Re: Replace two words in a file
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 04:56 AM
11-22-2002 04:56 AM
Re: Replace two words in a file
# grep -r -l 'Imp mode' . | xargs perl -pi -e's/\bImp mode\b/test mode/g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2002 06:10 AM
11-22-2002 06:10 AM
Re: Replace two words in a file
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.