BASH: how to substitute a expression with another without creating a new file
Linux - NewbieThis 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.
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.
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.
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.
$ 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
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.