Shell script find word from one file and insert in another file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell script find word from one file and insert in another file
# 1  
Old 08-24-2011
Shell script find word from one file and insert in another file

Hi,
I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file.
For example
The shell script search an IP address as parameter value from input.txt
------------------------------------------
vi input.txt
DUT_IPaddress=10.10.20.25
--------------------------------------------
And the shell script should insert the IP address in between two semicolons present at second line of output.txt file.
-------------------------------------------
vi output.txt
#this is configuration file
15952;;sunil
------------------------------------------
# 2  
Old 08-25-2011
Code:
#!/bin/bash

var=$(sed 's/.*=\(.*\)/\1/' < input.txt)
sed -i "/;;/s/\([^;]*;\)\(;[^;]*\)/\1$var\2/" output.txt

I have used sed's in-place edit '-i' option. If that is not available in your machine use redirection feature instead. I hope there will be more easier methods than my solution.
# 3  
Old 08-25-2011
Code:
IP_val=`awk -F= ' { print $2 } ' inputfile`
sed -n '2p' config_file | nawk -F";" -v IP="$IP_val" ' { print $1";"IP";"$3 } '

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script example to Grep matching word from file

Hi expert, Need help in shell script. a.txt file output is below i would like to grep 3 line and 1st column value which is admin\22226 only and not full line. i only know admin word as 22226 can come anything with admin\ in file. also after fetching it i would like to use this... (1 Reply)
Discussion started by: kuljeetpal
1 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. UNIX for Dummies Questions & Answers

Insert text into a file using shell script

Hi, I need to insert "Hello World" text into a file called hai.txt using shell scripting. Kindly help me. For eg: If I open the file hai.txt by giving linux command cat hai.txt, the content of the file should have the text Hello World in it. Thanks (5 Replies)
Discussion started by: karthick nath
5 Replies

4. Shell Programming and Scripting

How to insert a word into a text file?

Hi, I have a text file with one line having few words separated by space and I need to insert another word on "n"th column/field so that previous word should shift right (to n+1st column). how can I do that? It seems we can do using awk but unable to figure out. Please advise, thanks! ... (11 Replies)
Discussion started by: magnus29
11 Replies

5. Shell Programming and Scripting

Reading a word from a text file into shell script

Hi, I am new to shell programming.I need to write a script that would accept a word from each line fo an input text file.Can anyone help me with this?Exact requirement: word1 word2 word3 (separated by space) .Now I need word3 from each such line in the text file. Thanks in Advance, Manish (3 Replies)
Discussion started by: manish007
3 Replies

6. Shell Programming and Scripting

To search a file for a specific word in a file using shell script

Hi All, I have a sql output file has below. I want to get the values 200000040 and 1055.49 .Can anyone help me to write a shell script to get this. ACCOUNT_NO ------------------------------------------------------------ BILL_NO ... (8 Replies)
Discussion started by: girish.raos
8 Replies

7. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

8. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

9. Shell Programming and Scripting

insert word in each line of a file

can someone tell me how can I insert a word in front of each line in a file. i tried with sed but didn't managed yet. Is there another command or this one(sed) works? 10x anyway. (7 Replies)
Discussion started by: atticus
7 Replies

10. Shell Programming and Scripting

script to run shell command and insert results to existing xml file

Hi. Thanks for any help with this. I'm not new to programming but I am new to shell programming. I need a script that will 1. execute 'df -k' and return the volume names with specific text 2. surround each line of the above results in opening and closing xml tags 3. insert the results of step... (5 Replies)
Discussion started by: littlejon
5 Replies
Login or Register to Ask a Question