Visit Our UNIX and Linux User Community


shell script to search a string and delete the content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to search a string and delete the content
# 1  
Old 10-17-2008
shell script to search a string and delete the content

Hi,
I've a shell script e.g.

#!/bin/bash
echo "Enter the next hop id"
read nhid
echo "enter the IP address"
read IP
echo "enter the interface name"
read name
echo "enter the enable/disable state"
read state
exit 0

now from this script i want to search strings in another (.cam)
file e.g. the cam file is

BEGIN
next hop id value nhid
IP address value 192.168.1.101
Interface name xyz
enable disable state 1
END

now i've to search for the string 'next hop id value' in this cam file then delete the values in front of this string untill a new line starts and then append the user input value stored in the variable in front of this string.
similarly for other fields.

like if previously the cam file is as above and the user input value for ip address is 111.111.111.111, interface name is abc and enable/disable state is 0. finally this cam file should become like this:

BEGIN
next hop id value nhid
IP address value 111.111.111.111
Interface name abc
enable disable state 0
END


can anyone help me out in this.....
Thanks in advance!!

Vikas
# 2  
Old 10-17-2008
From the file shown, I'm assuming the values to be replaced, are the last in the record, otherwise they can easily be replaced with their actual location in
the record ( say $5, $8, $11...):

Code:
#!/bin/bash

read -p "Enter the next hop id:"           id
read -p "Enter the IP address: "           ip
read -p "Enter the interface name:"        intfc
read -p "Enter the enable/disable state:"  enab


awk -v id="$id" -v ip="$ip" -v int="$intfc" -v enb="$enab"  '/next hop id/    {$NF=id}
          						     /IP address/     {$NF=ip}
              						     /Interface/      {$NF=int}
            						     /enable disable/ {$NF=enb}1' file > file.$$ && mv file.$$ file

# 3  
Old 10-20-2008
Thanks for your help. can u give me its logic plz?
# 4  
Old 10-20-2008
thanks rubin for your support.
but somehow its not working. its happenning to delete all the content of the other file where i want to put the value.
& in fact, if i can make myself clear, i want to search a particular string(irrespective of its location in the other file) & then replace the conents in front of this particular string till a new line starts.
its not like appending the content in the end of file or appending something at a particular location.
Thanks a lot!!
# 5  
Old 10-20-2008
From your explanation it's quite unclear what you're trying to achieve, and it also seems that your pre-conditions have changed.
Could you post your code, and your efforts so far ?

BTW to append to a file you can use the >> operator.

Code:
[...your code ...] >> file

# 6  
Old 10-21-2008
Thanks Rubin for your response.
I've somehow arrived at a solution that is working for my requirement.

this is
sed "s/IPaddress .*/IPaddress $IP/g" my.cam>>my.cam.tmp && mv my.cam.tmp my.cam

& it searches the string 'IPaddress in the file my.cam from the existing script, deletes the contents after this string untill a new line starts and then appends the value of variable 'IP' in front of this string 'IPaddress'

this was the exact thing i wanted to achieve.

But,thanks for all your support.

Vikas
# 7  
Old 10-21-2008
Quote:
Originally Posted by vic_mnnit
Thanks Rubin for your response.
I've somehow arrived at a solution that is working for my requirement.
...
this was the exact thing i wanted to achieve.
Glad that you've found the solution to your problem.

Quote:
But,thanks for all your support.

Vikas
Welcome.

Previous Thread | Next Thread
Test Your Knowledge in Computers #872
Difficulty: Medium
In computer science, self-modifying code is code that alters its own instructions while it is executing.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Search a string in a file in shell script?

I have a text file which is generated when the batch job is run. This batch may take few mins to run. When completed, the last line of the text file would be process completed. I need a shell script which will wait for this file till the process completed is printed in it, once found, it would move... (2 Replies)
Discussion started by: Lalat
2 Replies

2. Shell Programming and Scripting

Linux shell script, search by an input string

So, there is a large file where I have to conduct several search using bash shell scripting. The file is like this: TITLE and AUTHOR ETEXT NO. Aspects of plant life; with special reference to the British flora, 56900 by Robert Lloyd... (1 Reply)
Discussion started by: Philia
1 Replies

3. Shell Programming and Scripting

Shell script to search all files for every string in another file

Hello All I have a pattern.txt file in source directory ((/project/source/) in linux server and data looks like: 123abc17 234cdf19 235ifg20 I have multiple log files in log directory (/project/log/) in linux server and data for one log file looks like: <?xml version="1.0" processid... (11 Replies)
Discussion started by: pred55
11 Replies

4. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

5. Shell Programming and Scripting

UNIX shell script to search a string in a file

Hi folks, I am new for shell script, I hope somebody to help me to write shell script My requirement is below steps 1. I have apache access.log i.e located in /var/log/httpd/ Ex. 127.0.0.1 - - "GET... (14 Replies)
Discussion started by: Chenchireddy
14 Replies

6. Shell Programming and Scripting

Conditional search and delete using SED / Shell script

Hi, I want to perform a conditional search and remove my search string. Input string: "abcdaabcadgfaarstab" Character to search: "a" Condition: Remove all "a" in the input string except if it is "aa" Output string: "bcdaabcdgfaarstb" Can you please help me in this? (5 Replies)
Discussion started by: dominiclajs
5 Replies

7. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

Have to read one file say sourcefile containing several words and having another folder containing several files. Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Discussion started by: mukesh.baranwal
3 Replies

8. Shell Programming and Scripting

Search for string in filename, not file content

How can I search for a string in a filename? For example, I want to know if a filename (not the file contents) contains the string "test". (3 Replies)
Discussion started by: daflore
3 Replies

9. Shell Programming and Scripting

Delete content of file 1 in file 2 with shell script

OK, best is I explain what the operating enviroment is. Linux, but Motomagx. It is a Linux operated mobile phone, Motorola V8. I am writting a shell script, but got stuck. I have to delete the complete content of file 1 in file 2. I have attached the 2 files. You can see that the content of... (2 Replies)
Discussion started by: rasputin007
2 Replies

10. Shell Programming and Scripting

shell script to search content of file with timestamps in the directory

hello, i want to make a script to search the file contents in my home directory by a given date and output me the line that has the date... (10 Replies)
Discussion started by: psychobeauty
10 Replies

Featured Tech Videos