[SOLVED] How can I replace a string based on a pattern on a line?
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.
That link has pretty good explanation, but you would be better served to learn e.g. sed (or awk or whatever) rather than just asking for particular answers to a one-off problem.
Both are well documented.
I want to change usb04 to usb03 if the line has the pattern Dodge City.
I did a google search on this but I get results contrary to what I want.
eg sed 's/usb03/Dodge City/' videos
This is NOT what I want. This will change Dodge City to usb03.
No, that is wrong. that will change usb03 to Dodge City.
Anyway, the link has a very good example.
Quote:
Originally Posted by geekoo
Is that possible with sed, awk or other command?
Yes, definitely, almost any language can do that (including bash, java, perl, python, ruby, c, ....). And also there are several different ways to solve it.
It did what I wanted and I committed the change by adding sed -i
Be aware that this has the potential to destroy your file. Fine if you check it is ok first, but better perhaps to include a suffix to ensure a backup is created automatically for you.
Be aware that this has the potential to destroy your file. Fine if you check it is ok first, but better perhaps to include a suffix to ensure a backup is created automatically for you.
With bash imternals you need a while-read loop to also process the other lines
Code:
while IFS= read -r line
do
if [[ "$line" == *"Dodge City"* ]]; then
printf "%s\n" "${line//usb04/usb03}";
else
printf "%s\n" "$line"
fi
done < inputfile > outputfile
a="usb04 Dodge City (1939)"
#a="usb04 Doge City (1939)"
if [[ "$a" == *"Dodge City"* ]]; then
echo ${a//usb04/usb03};
else
echo "$a"
fi
Quote:
Originally Posted by MadeInGermany
With bash imternals you need a while-read loop to also process the other lines
Code:
while IFS= read -r line
do
if [[ "$line" == *"Dodge City"* ]]; then
printf "%s\n" "${line//usb04/usb03}";
else
printf "%s\n" "$line"
fi
done < inputfile > outputfile
Thanks guys for the extra info. However, it's alot of code to write where I can do the same with a sed one-liner.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.