pipe output of grep to sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pipe output of grep to sed?
# 1  
Old 07-01-2009
pipe output of grep to sed?

Is there a way I can do this:

search for text and replace line containing matched text with a different line?
For example:

"I want to replace text"

I want to search for replace and then change the line to

I am perplexed.

Hope that makes sense.

Thanks in advance.
# 2  
Old 07-01-2009
You can try this:

grep replace | sed 's/replace/perpleaxed/g'

Or you want to relpace line then;
grep replace | sed 's/I want to replace text/I am perplexed/g'
# 3  
Old 07-01-2009
You don't need to use grep at all. sed would be enough.

Code:
# Replace the whole line with a new one
sed -e "s/.*old_string.*/new_string/"

# Replace the chosen string with a new one
sed -e "s/old_string/new_string/"

# Replace every occurance of the chosen string with a new one
 sed -e "s/old_string/new_string/g"

# 4  
Old 07-01-2009
Let me be more specific here:

I have two variables in a shell script that I assign by reading contents of a file
The contents are as such
a="$var1=xyz"
b="$var1=abc"

I need to search a second file for occurences of $var1=xyz and replace it with
$var1=abc

I have used sed -s 's/'"$a"'/'"$b"'/g' which works fine but whenever the contents of variable a contain some special characters for example a="$var1=fdh&%4"
the replace is not working. If I change the value to just a="$var1=", the above sed works successfully to replace $var1= in the file to $var1=abc.

Can anybody please help?
# 5  
Old 07-01-2009
please put a backlash "\" before special characters

sed 's/\*/\@/g'


replaces * with @
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

2. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

3. Shell Programming and Scripting

Grep:pipe delimited output

Hi, I am trying to search for a string in a file and print all the matched lines as pipe delimited format. My command is cat m_gid_trans.XML|grep -i '<TABLEATTRIBUTE NAME ="Lookup cache directory name"' The output I am getting is <TABLEATTRIBUTE NAME ="Lookup cache directory name"... (4 Replies)
Discussion started by: sampoorna
4 Replies

4. Shell Programming and Scripting

Grep & sed - Output

Hi All, Facing an issue with grep & sed I have logs as below: gsc_1_20121121.log:2012-11-21 10:09:13,143 INFO - fmsspace.1 ProcessNewOrderSingleRequest: Result - ProcessNewOrderSingleBatchResultDTO - success:true,newOrderSingleBatchResults:ProcessNewOrderSingleResultDTO -... (13 Replies)
Discussion started by: irfanmemon
13 Replies

5. Shell Programming and Scripting

Pipe awk's output to sed for deletion

Hi Friends, I am using a command that prints certain lines from a file. For ex: cat input abc chr1 456 def chr1 789 ghi chr1 999 jjj chr1 777 jhk chr7 914 My command awk '{if($2=="chr1" && $3>=456 && $3<=999) {print $0}}' OFS="\t" input Output being printed is abc chr1 456 (7 Replies)
Discussion started by: jacobs.smith
7 Replies

6. Shell Programming and Scripting

Display output as columns using grep/awk/sed

I have several files with say something like cat sparrow I can grep for "cat" and "sparrow" and usually the output is one below the other cat sparrow How can I view these as columns say Pets Birds cat sparrow Would be great if this can be on command line using awk or... (1 Reply)
Discussion started by: gubbu
1 Replies

7. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

8. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

9. UNIX for Dummies Questions & Answers

Using grep output as input for sed command

Hi, I would like to know if this is possible, and if so what can i do to make this work. I would like to grep a line X from fileA and then use the output to replace a word Y in fileB. grep "line X" fileA | sed -e 's/Y/X/g' > outfile this statement does not work, as i do not know how to... (7 Replies)
Discussion started by: cavanac2
7 Replies

10. Shell Programming and Scripting

pipe'ing grep output to awk

This script is supposed to find out if tomcat is running or not. #!/bin/sh if netstat -a | grep `grep ${1}: /tomcat/bases | awk -F: '{print $3}'` > /dev/null then echo Tomcat for $1 running else echo Tomcat for $1 NOT running fi the /tomcat/bases is a file that... (2 Replies)
Discussion started by: ziggy25
2 Replies
Login or Register to Ask a Question