LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-20-2022, 02:37 AM   #1
geekoo
LQ Newbie
 
Registered: May 2022
Posts: 26

Rep: Reputation: 3
How can I replace a string based on a pattern on a line?


Hi

I have a line in a file with this string

usb04 Dodge City (1939)

I want to change usb04 to usb03 if the line contains the pattern Dodge City.

There are many lines in the file with usb04. I only want to change one only.


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.

I want to change usb04 to usb03 if the line has the pattern Dodge City.

Is that possible with sed, awk or other command? Thanks

Last edited by geekoo; 08-20-2022 at 02:44 AM.
 
Old 08-20-2022, 02:55 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Quote:
Is that possible with sed, awk or other command?
Yes.
 
1 members found this post helpful.
Old 08-20-2022, 03:09 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
1 members found this post helpful.
Old 08-20-2022, 03:59 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by geekoo View Post
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 View Post
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.
Code:
sed 's/usb04 Dodge City/usb03 Dodge City/'
 
1 members found this post helpful.
Old 08-20-2022, 04:27 AM   #5
geekoo
LQ Newbie
 
Registered: May 2022
Posts: 26

Original Poster
Rep: Reputation: 3
Thanks guys

I got it.

I tried it first.
Code:
sed '/Dodge City/s/usb04/usb03/' videos
It did what I wanted and I committed the change by adding sed -i

I just click yes on all replies as helpful answers. Thanks allend for pointing me to the source to learn it.

Last edited by geekoo; 08-20-2022 at 04:31 AM.
 
Old 08-20-2022, 05:30 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by geekoo View Post
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.
 
1 members found this post helpful.
Old 08-20-2022, 06:28 AM   #7
geekoo
LQ Newbie
 
Registered: May 2022
Posts: 26

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by syg00 View Post
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.
That's a great tip.
 
Old 08-20-2022, 10:11 AM   #8
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
I want to change usb04 to usb03 if the line contains the pattern Dodge City.
You could also do that with regex.
Code:
a="usb04 Dodge City (1939)"

#a="usb04 Doge City (1939)"

if [[ "$a" == *"Dodge City"* ]]; then
    echo ${a//usb04/usb03};
else
    echo "$a"
fi
 
1 members found this post helpful.
Old 08-20-2022, 01:27 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
 
1 members found this post helpful.
Old 08-21-2022, 01:30 AM   #10
geekoo
LQ Newbie
 
Registered: May 2022
Posts: 26

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by teckk View Post
You could also do that with regex.
Code:
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 View Post
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.

but it's good to know.
 
Old 08-21-2022, 01:56 AM   #11
geekoo
LQ Newbie
 
Registered: May 2022
Posts: 26

Original Poster
Rep: Reputation: 3
Does anyone know how to make Dodge City case insensitive in the search?

I purposely changed Dodge City to lowercase to test case insensitive, but it's not working.

I tried with the i switch (red) in two locations but it doesn't work.

Code:
$ sed '/Dodge City/s/usb04/usb03/i' videos
usb04 	dodge city (1939)

$ sed '/Dodge City/is/usb04/usb03/' videos
usb04 	dodge city (1939)
How can I make Dodge City be case sensitive? The link doesn't show how. Thanks

Last edited by geekoo; 08-21-2022 at 01:58 AM.
 
Old 08-21-2022, 02:22 AM   #12
geekoo
LQ Newbie
 
Registered: May 2022
Posts: 26

Original Poster
Rep: Reputation: 3
I got it. To make Dodge City case insensitive. I had to use a capital I, not a lowercase i.

Code:
$ sed '/Dodge City/Is/usb04/usb03/' videos
usb03 	dodge city (1939)
 
1 members found this post helpful.
Old 08-21-2022, 03:05 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I'd suggest you go here and download the sed manual in your preferred format. It is a reference, but also has examples to explain functionality.
 
1 members found this post helpful.
Old 08-21-2022, 04:39 AM   #14
geekoo
LQ Newbie
 
Registered: May 2022
Posts: 26

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by syg00 View Post
I'd suggest you go here and download the sed manual in your preferred format. It is a reference, but also has examples to explain functionality.
This is very helpful. Thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
Replace a string with pattern within that string shreyas08 Linux - Newbie 4 03-14-2012 12:07 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration