LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Search for a pattern and if it exists change some other text on the line (https://www.linuxquestions.org/questions/linux-newbie-8/search-for-a-pattern-and-if-it-exists-change-some-other-text-on-the-line-4175458192/)

KRevotsk 04-15-2013 09:12 AM

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.

TIA,
Kathy

druuna 04-15-2013 09:22 AM

You don't need 2 sed parts, have a look at this:
Code:

sed '/onnection/s/Abc/Xyz/g' $file
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

These links might help:

Sed resources:

konsolebox 04-15-2013 09:35 AM

What's the actual error?

Try this version:
Code:

file="first_file.exp"
sed -n "/onnection/p" "$file" | sed -e "s/_Abc/_Xyz/g" > "${file}_test1_parsed"

Edit: I think you don't have to pass the file on the second sed command.

druuna 04-15-2013 09:56 AM

@konsolebox: Why 2 piped sed commands in the first place?

konsolebox 04-15-2013 10:47 AM

@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.

druuna 04-15-2013 10:54 AM

And I focussed too much on the range stuff.

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

konsolebox 04-15-2013 11:00 AM

Quote:

Originally Posted by druuna (Post 4931920)
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.

KRevotsk 04-15-2013 12:36 PM

Thanks all. Great suggestions! I do want all the lines in the file not just the ones I have changed.

KRevotsk 04-15-2013 03:31 PM

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?

druuna 04-15-2013 03:39 PM

Sed has a -i for in place changes (changes the original file):
Code:

sed -i -n '/onnection/s/Abc/Xyz/gp' $file

# or, create backup first
sed -i.bak -n '/onnection/s/Abc/Xyz/gp' $file

If you want to put the output in a different file:
Code:

sed -n '/onnection/s/Abc/Xyz/gp' $file > $newfile

KRevotsk 04-16-2013 08:31 AM

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.

konsolebox 04-16-2013 08:39 AM

Quote:

Originally Posted by KRevotsk (Post 4932038)
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:
Code:

sed '/onnection/s/Abc/Xyz/g' "$file" > "$newfile"

KRevotsk 04-16-2013 09:05 AM

Got it! Thanks all. I am all set.

konsolebox 04-16-2013 09:10 AM

Welcome. Glad I was able to help, miss :)

David the H. 04-18-2013 02:23 PM

By the way, as for the actual error in the OP:

Code:

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.)


PS: Don't forget to quote all of your variables.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
[url]http://mywiki.wooledge.org/Quotes[url]

Edit:

Quote:

I tried -i but I get sed: illegal option -- i

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.


All times are GMT -5. The time now is 11:41 AM.