sed search and replace word assistance...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed search and replace word assistance...
# 1  
Old 12-04-2008
sed search and replace word assistance...

Hi,
I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file:

1.ejverything
2.bllown
3.maikling
4.manegement
5.existjing
6.systems

My design currently takes input from the user, and stores it in a variable (linenum) which is then used in sed as the line number. I have,


echo "Which line number"
read linenum

And then use $linenum in sed as the line number in the file, ex

sed '"$linenum"i\
> FINDSTRING\
> REPLACESTRING
> ' foo > foo.old

I haven't got the above to work so far. I guess my question is,
can you use variables in a sed command line? If so how?

I also saw the option of using a loop to accomplish the job somewhere
as shown below...

#!/bin/bash
for in $fl; do
mv $fl $fl.old
sed 's/FINDSTRING/REPLACESTRING/g' $fl.old > $fl
rm -f $fl.old
done


Please help!
Image
# 2  
Old 12-04-2008
# 3  
Old 12-04-2008
Please read the rules - no duplicate threads.
Thread closed!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Assistance required to decode sed search using /1

Hi, I am trying to extract line number (first number), as well as everything from TSVal onwards. 4 1.474005 172.18.124.142 -> 74.125.228.46 TCP 2450940617 74 44021 > https Seq=0 Win=5840 Len=0 MSS=1380 SACK_PERM=1 TSval=2450940617 TSecr=0 WS=64 6 1.488149 172.18.124.142 ->... (1 Reply)
Discussion started by: sand1234
1 Replies

2. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

3. UNIX for Dummies Questions & Answers

Search for a particular word and replace the first character

Hi Unix gurus, I've a dna sequence in a file format known as fasta format (sequence header starts with > and ignored), an example shown below: >sequence_1 CGTATTCTCCGAATACC ATACG >sequence_2 CAGATTTTCAAATACCCCC In a file like this I want to do the following three search and replace. The... (4 Replies)
Discussion started by: Fahmida
4 Replies

4. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

5. Shell Programming and Scripting

Replace a word using sed

Hi, I have the following line in file1 export NAME="NEW_NAME" I'm writing a shell script which reads the NEW_NAME from the user and replace in the file. I have used the following command for that read Name_replace sed -i 's/NEW_NAME/$Name_replace/' file1 but it is not... (2 Replies)
Discussion started by: Ananthdoss
2 Replies

6. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

7. UNIX for Dummies Questions & Answers

search and replace with sed

Hi All I have a simple text file and I want to be able to replace any alpha character and comma combination with any other symbol of my choice here is the text file I want to replace: pear apple ban,ana grape ",g1234," te,st1 here is how it should look afterwards: pear... (4 Replies)
Discussion started by: greg_b
4 Replies

8. Shell Programming and Scripting

Replace word with sed

Hi there.! I'm trying to make a script that corrects wrong spelling. I want to use sed to replace wrong word with the correct one, but this must be made in a while loop that reads the wrong word from file (with read line) and the correct one from another file. I can't find a way to run sed like... (3 Replies)
Discussion started by: spiii
3 Replies

9. Shell Programming and Scripting

Search for word in a xml file and replace it with something else

Hello Unix Users, I am very new to Unix so I am not sure how do I do the following. I need a script such that when I type the following in the command prompt > . scriptName.sh wordToBeReplaced DirectoryLocation will find the word someword located in a somefile.xml in DirectoryLocation... (8 Replies)
Discussion started by: 5211171
8 Replies

10. Shell Programming and Scripting

Sed - need to replace a word by another in 82 files.

HI everyone ;) I looking for help to use sed. I have 82 files. In each file I need to replace a word by another. All files are on the same directory.all files are texte files. I need to keep the same file name for all file that will be modified. Here is an exemple : file name :... (7 Replies)
Discussion started by: Aswex
7 Replies
Login or Register to Ask a Question