Using grep output as input for sed command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using grep output as input for sed command
# 1  
Old 04-07-2010
Data 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.

Code:
grep "line X" fileA | sed -e 's/Y/X/g' > outfile

this statement does not work, as i do not know how to get the sed command to recognize the grep output.

Any suggestions?

edit by bakunin: please use code-tags for posting code, commands or file contents

Last edited by bakunin; 04-07-2010 at 01:07 PM..
# 2  
Old 04-07-2010
Hi,

The below command is working for Korn shell:

HTML Code:
grep "AAA" file1.txt | sed 's/A/Z/g' > output.txt
Following is the output of the command:

HTML Code:
$ cat file1.txt
AAA
BBB
CCC
DD
EE
$ grep "AAA" file1.txt | sed 's/A/Z/g' > output.txt
$ cat output.txt
ZZZ
Hope this will help you. Smilie
Thanks!
# 3  
Old 04-07-2010
Using grep output as input for sed command

Hi,

Thanks much, unfortunatly, its not quite what i need. I need to be able to go into my FileB and using the grep output from FileA do a replace command on one of my words in file A with the grep output.

I forgot to add in the fileB in my last line of code, but heres what i was going for
Code:
grep "line X" fileA | sed -e fileB 's/Y/X/g' > outfile

again not sure if there is a way to be able to use the grep output in the sed command.

Best,
Claire

Last edited by bakunin; 04-07-2010 at 01:07 PM..
# 4  
Old 04-07-2010
you haven't supplied an example, but i suppose the output of grep can be multiple lines, yes? If so:

Code:
#! /bin/ksh

grep "Expr" /path/to/file | while read source ; do
     print - "now replacing \"$source\" with \"target\""
     sed 's/'"$source"'/target/' /path/to/file2 > /path/to/file2.tmp
     mv /path/to/file2.tmp /path/to/file2
done

This will grep the file "/path/to/file" for lines containing the expression "Expr" and feed every line found this way into the variable "$source", which in turn is successively searched for in file /path/to/file2 and replaced by "target". Notice the quoting of the variable inside the sed-line, which is necessary to make sure the resulting command is a continuous string. The print command is only meant as an explanatory device and not necessary.

I hope this helps.

bakunin
# 5  
Old 04-07-2010
Bug Using grep output as input for sed command

Thank you so much, this has made my day.

Just had one adjustment, and that was to switch the 'target' and the 'source' for the find and replace to work correctly (",)

Code:
#! /bin/ksh

grep "Expr" /path/to/file | while read source ; do
       print - "now replacing \"$source\" with \"target\""
       sed 's/target/'"$source"'/' /path/to/file2 > /path/to/file2.tmp
       mv /path/to/file2.tmp /path/to/file2
done


Really appreciate your help, thanks again

edit by bakunin: please use the code-tags i supplied in your post yourself henceforth. Thank you.

Last edited by cavanac2; 04-08-2010 at 06:50 AM.. Reason: to keep original code supplied by previous reply
# 6  
Old 04-08-2010
Ahem, ...

As much as i appreciate your appreciation i have a feeling that the reasoning behind your script is somewhat flawed: When you first run the loop the sed-statement will change all occurrences of "target" to whatever the grep gave as first hit. When the loop is executed for the second (or any subsequent) time it would try to change "target" to whatever grep provided as secod hit, but there will be no more instances of "target", because they all have been changed in the first run, yes?

This will only work if any subsequent expression will hit a subset of the previous one, which has just got changed. In this case i suppose there would be more efficient (and more elegant) ways to achieve the same result. How about you state your complete problem here and we have a review?

I hope this helps.

bakunin
# 7  
Old 04-08-2010
Using grep output as input for sed command

you will have to forgive me, i am still very much in the beginning learning stages here

Basically i have a file1 which has one line of information which i need to place into a specific position in file2, lets call this line XXX
I wanted to 'grep' the XXX line from file1 and then pipe it to file2. In file 2 i had a specific location that i wanted XXX to go into, this location i labeled in my file as YYY.
Where source = 'XXX' and Target='YYY'

Code:
#! /bin/ksh

grep "XXX" /path/to/file | while read source ; do
       print - "now replacing \"$source\" with \"target\""
       sed 's/YYY/'"$source"'/' /path/to/file2 > /path/to/file2.tmp
       mv /path/to/file2.tmp /path/to/file2
done

This seems to work quite well, as it goes into my file2 and replaces 'YYY' with 'XXX' from the original file1.

i hope that is ok?

Best
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

Grep command to search pattern corresponding to input from user

One more question: I want to grep "COS_12_TM_4 pattern from a file look likes : "COS_12_TM_4" " ];I am taking scan_out as the input from the user. How to search "COS_12_TM_4" in the file which is corresponds to scan_out (12 Replies)
Discussion started by: Preeti Chandra
12 Replies

3. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

4. Shell Programming and Scripting

Script output as input for next command

Hi All, Hoping you can help as im in desperate need... I'm very new to unix scripting so apoligies, I have setup an expect script in order to log into a node on our network, This will provide an output as per the below *********** information: *************: n/a TEST IP : n/a ... (18 Replies)
Discussion started by: mutley2202
18 Replies

5. Shell Programming and Scripting

Command Output to Standard Input

Hi All, How do I provide the output of a command to another command which is waiting for an input from the user ? Ex : I need to login to a device via telnet. In the script, initially I use the "read" command to get the IP Address, Username and Password of the device from the user. Now,... (1 Reply)
Discussion started by: sushant172
1 Replies

6. UNIX for Dummies Questions & Answers

Send output of grep as input of kill command

I would appreciate any help. I need to run 'ps -ef | grep 'process', get the process id and kill that process. I have got this far: - Get pid using ps -ef | awk '/process/{ print $2}' after this I'm kind of stuck.. - Use pipe to redirect the output to kill pid=ps -ef | awk '/bmserver/{... (2 Replies)
Discussion started by: foxtron
2 Replies

7. Shell Programming and Scripting

how to input the CAT command output in Shell

Hi guys... I am new to this scripting...so please forgive me if anything worng in my questions... here is my question.. I have file structure /home/oracle/<sid>/logs/bkup now i want to write a script which should grep the sid name from a file..and it should replace the <SID> with... (1 Reply)
Discussion started by: troubleurheart
1 Replies

8. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

9. Shell Programming and Scripting

Using output to input another command

Hi guys. Is it possible (I'm sure it is) to use the output of a simple 'ls' command as input of another command 'tail'. It is not really the output of the 'ls'. I have to useeach line of the output. This is the first command... ls *myFile*021308* Which it outputs many filenames. For each... (3 Replies)
Discussion started by: rodrimuino
3 Replies

10. Shell Programming and Scripting

Using Output from one command as input to another

This site has been very helpful thus far.. I thank you all in advance for sharing the knowledge. Let me get to it. I am trying to write a very small script to take away from the boredom of doing the same thing over and over. Everynow and again I have to get the hex value of a file using a... (2 Replies)
Discussion started by: BkontheShell718
2 Replies
Login or Register to Ask a Question