Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-16-2009, 09:58 AM
|
#1
|
Member
Registered: Oct 2009
Posts: 40
Rep:
|
BASH: how to substitute a expression with another without creating a new file
Hi guys,
I'm writting a script in BASH and I want to find the expression "int foo[i] = true;" in a text and substitute it by the word "boo[i]= true;", but without erasing other words from the text and without having to create a new file. Would somebody know how I can do that?
Text for example:
whi(&M§)/&§M)POM;M;(29073()/§()"/§;();§/()"7§ZE/§)&§=)"&/=)$&=!&$=!
waxe83qmi3u,jy,einure,xl9me23uy9, int foo[i] = true;i23904802y97p,94280
u2908/M="(§?;$(/EZ"Zolsdwiea,ru8
q2xi4l9i<ey93
2x5093405
int foo[i]= true;
39i2e09ui9,y92ßi049.
|
|
|
11-16-2009, 10:19 AM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
sed -i -e 's/int\ foo\[i\]=true\;/something\ else/g' myfile
TBH, the escape characters mightn't be exactly correct, but it's the -i option for sed you're really looking for.
|
|
|
11-16-2009, 10:30 AM
|
#3
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
Hi Cris!
That works perfectly. Thanks a lot
Actually would you also know how can I tell 'sed' to substitute just the first match?
Last edited by carolflb; 11-16-2009 at 10:33 AM.
|
|
|
11-16-2009, 12:03 PM
|
#4
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
well the "g" makes sed do multiple replacements in the same line, so if you remove that it'll only do it once per line.
|
|
|
11-16-2009, 10:35 PM
|
#5
|
LQ Newbie
Registered: Oct 2008
Posts: 13
Rep:
|
Why execute sed as an external process when bash can just do it inline?
Code:
#! /bin/bash
match='int foo\[i\] = true;'
while read line
do
[[ "$line" =~ "$match" ]] && line="${line/$match/something else}"
echo "$line"
done
Quote:
${parameter/pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. If Ipattern
begins with /, all matches of pattern are replaced with string.
Normally only the first match is replaced. If pattern begins
with #, it must match at the beginning of the expanded value of
parameter. If pattern begins with %, it must match at the end
of the expanded value of parameter. If string is null, matches
of pattern are deleted and the / following pattern may be omit-
ted. If parameter is @ or *, the substitution operation is
applied to each positional parameter in turn, and the expansion
is the resultant list. If parameter is an array variable sub-
scripted with @ or *, the substitution operation is applied to
each member of the array in turn, and the expansion is the
resultant list.
|
|
|
|
11-16-2009, 10:55 PM
|
#6
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
$ more file
whi(&M§)/&§M)POM;M;(29073()/§()"/§;();§/()"7§ZE/§)&§=)"&/=)$&=!&$=!
waxe83qmi3u,jy,einure,xl9me23uy9, int foo[i] = true;i23904802y97p,94280
u2908/M="(§?;$(/EZ"Zolsdwiea,ru8
q2xi4l9i<ey93
2x5093405
int foo[i]= true;
39i2e09ui9,y92ßi049.
int foo[i] = true
the end
$ awk '{gsub(/int[ ]+foo\[i\][ ]*=[ ]*true/,"---")}1' file
whi(&M§)/&§M)POM;M;(29073()/§()"/§;();§/()"7§ZE/§)&§=)"&/=)$&=!&$=!
waxe83qmi3u,jy,einure,xl9me23uy9, ---;i23904802y97p,94280
u2908/M="(§?;$(/EZ"Zolsdwiea,ru8
q2xi4l9i<ey93
2x5093405
---;
39i2e09ui9,y92ßi049.
---
the end
|
|
|
11-16-2009, 10:58 PM
|
#7
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by stevenworr
Why execute sed as an external process when bash can just do it inline?
Code:
#! /bin/bash
match='int foo\[i\] = true;'
while read line
do
[[ "$line" =~ "$match" ]] && line="${line/$match/something else}"
echo "$line"
done
|
these will not match:
Code:
1) int foo[i]= true
2) int foo[i] = true
3) int foo[i] =true
|
|
|
11-17-2009, 01:09 AM
|
#8
|
LQ Newbie
Registered: Oct 2008
Posts: 13
Rep:
|
Quote:
Originally Posted by ghostdog74
these will not match:
Code:
1) int foo[i]= true
2) int foo[i] = true
3) int foo[i] =true
|
You have to give bash what it wants to do the job. You can make this as hard as you want or only as hard as you need.
Code:
match=$'int[ \t]+foo\[i\][ \t]*=[ \t]*true[ \t]*;'
Then you can do stuff like
Code:
[[ "$line" =~ "$match" ]] && line="int boo[i] = true;
Feel free to add as many extra paren groupies to exactly preserve as much whitespace as you need.
If you really want to go ape
Code:
match=$'int([ \t]+)foo\[i\]([ \t]*=[ \t]*)true([ \t]*);'
[[ "$line" =~ "$match" ]] && line="int${BASH_REMATCH[1]}boo[i]${BASH_REMATCH[2]}true${BASH_REMATCH[3]};"
Note that I am using $'stuff' to embed a tab in a string and I use the BASH_REMATCH array to access the parts of the match that I care to reproduce. It's all in the bash man page.
|
|
|
11-18-2009, 09:42 AM
|
#9
|
Member
Registered: Oct 2009
Posts: 40
Original Poster
Rep:
|
Hi guys,
first thanks for all replies
I think I didn't express myself the right way. What I wanted to ask with "would you also know how can I tell 'sed' to substitute just the first match?" was that I want to know if it's possible to use 'sed' to erase or substitute just the first occurency of the searched pattern in the file, so if I have:
text
text PATTERN
text
PATTERN
text
PATTERN
text
So, is there a way of substituting the first occurency of PATTERN in the file and erasing the other ones? So that at the end I have:
text
text SUBSTITUTED PATTERN
text
text
text
|
|
|
All times are GMT -5. The time now is 03:55 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|