[SOLVED] Search for a pattern and if it exists change some other text on the 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.
Search for a pattern and if it exists change some other text on the line
I am very new to sed and cannot seem to get my command to work. I am trying to search each line for the value 'onnection' and if it exists anywhere in the line change Abc to Xyz and write the line to a new file which is a parameter passed to the shell. My code currently looks like this
file="first_file.exp"
sed -n "/onnection/p" $file|sed -e "s/_Abc/_Xyz/g" $file > "$file_test1_parsed"
What am I doing wrong? It is changing all occurrences of Abc not just the lines that have connect in them.
The blue part selects lines with onnection in them and if so Abc is turned into Xyz
You can tell sed to use line or pattern ranges as well:
Code:
sed '4,$s/X/Y/g' # Change X to Y from line 4 to last line
sed '/strngA/,/stringB/s/X/Y/g' # change X to Y from line containing stringA to line containing stringB
@druuna I missed your post as I had my focus on the variables and jumped quickly on it sorry. After making a quick notice I just made a quick edit of my answer.
Anyway I think you should adjust the command to this:
Code:
sed -n '/onnection/{ s/Abc/Xyz/g; p; }' "$file"
I don't think the OP wants the other lines as described.
If only the lines need to be printed that contain onnection, it can be done like this (simpler?) as well:
Code:
sed -n '/onnection/s/Abc/Xyz/gp' $file
I see. I don't really try to remember that s accepts p subcommand, so I just chose the 'g; p;', but I actually had the idea before I modified it. Just to be safe.
I tried -i but I get sed: illegal option -- i
When I use the syntax suggested above sed -n '/onnection/s/Abc/Xyz/gp' $file > $newfile
all I get in newfile are the changed lines and I want newfile to contain the whole file containing the changed and unchanged lines.
What would I change in sed -n '/onnection/s/Abc/Xyz/gp' $file to get all the lines from $file to newfile with my changes?
My suggestion was actually to just print the lines that contain the pattern. If you want to include other lines as well, you could just use druuna's first post:
sed -n "/onnection/p" $file | sed -e "s/_Abc/_Xyz/g" $file > "$file_test1_parsed"
The first sed is reading from the file, and then the second sed is also reading from the same file. The piped data between them is being ignored. Remove the "$file" argument from the second command and that should fix it.
(Although as shown you you only really need a single command.)
Only the gnu version of sed included in most Linux distros has the -i option. You're apparently using a different implementation that doesn't provide it. Check your man page.
Last edited by David the H.; 04-18-2013 at 02:27 PM.
Reason: as stated
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.