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
# 8  
Old 04-08-2010
First off, thank you for using the code-tags. Yours didn't work because you can enter them either all caps ("CODE") or all small ("code"), but not mixed case. I changed that for you.

Regarding your problem: The loop would cover the case when your file1 would contain several lines and you use grep to single out ONE OR SEVERAL of them and replace any occurrence in file two with one after the other. Lets do an example:

File 1
Code:
use first
use second
ignore third
use fourth
ignore fifth

File 2
Code:
this is target and this
line also contains target (2)
and some more lines follow here where
target (3) is mentioned and so on
with target (4).

Program:
Code:
grep "use" file1 | while read source ; do
       print - "now replacing \"target\" with \"$source\""
       sed 's/target/'"$source"'/' file2 > file2.tmp
       mv file2.tmp file2
done

When you only execute the "grep 'use' file1" you will see that it filters out all the lines starting with "ignore" and only displays the one with "use". The variable "source" will be given each of these lines as a content successively at every single execution of the loop:

1) set variable source to "use first"
2) execute "sed '/target/use first/' file2 > file2.tmp"
3) execute mv file2.tmp file2
4) set variable source to "use second"
5) execute "sed '/target/use second/' file2 > file2.tmp"
6) execute mv file2.tmp file2
7) set variable source to "use fourth"
8) execute "sed '/target/use fourth/' file2 > file2.tmp"
9) execute mv file2.tmp file2
...and so on

As you can see all the occurrences of "target" will be changed to "use first" in step 2. Step 5, step 8, etc. will have nothing to do at all.

Therefore the only sensible reasoning for your problem is that "file1" contains only one line from the beginning (that is: only one line which is matched by the grep). In this case you don't need to loop at all (because a loop which is executed only once anyway is unnecessary) and therefore you could rewrite your script like this:

Code:
source="$(grep 'XXX' /path/to/file1)"
sed 's/target/'"$source"'/g' /path/to/file2 > /path/to/file2.tmp
mv /path/to/file2.tmp /path/to/file2

or, even shorter:

Code:
sed 's/target/'"$(grep 'XXX' /path/to/file1)"'/g' /path/to/file2 > /path/to/file2.tmp
mv /path/to/file2.tmp /path/to/file2

I hope this helps.

bakunin
 
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