sed command - substitue first instance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command - substitue first instance
# 8  
Old 04-05-2006
hi vgersh99

sed -e 's/pattern/REPLACEMENT/1' <INPUTFILE

i tried your code already. but it does not work.
can u test it on your side once? please.

and one more thing
is it possible to replace first instance of search patternt by using sed command???

is it possible to replcae first insatnce by using sed command?
if its not possible we will close this thread.. instead of [w]/investing our time.
# 9  
Old 04-05-2006
Quote:
Originally Posted by vgersh99
Code:
sed -e 's/pattern/REPLACEMENT/1' <INPUTFILE

i tried with this one and it didnot work,
Code:
>cat filename
swap
swap1
swap2
swap
swap

ur command
Code:
sed -e 's/swap/newswap/1' < filename

and this is the output of ur command
Code:
newswap
newswap1
newswap2
newswap
newswap

try this command,
Code:
awk 'BEGIN{x=0} /swap/ {if (x==0) { print "replace"; x=1} else { print } }' filename

and this is the output,
Code:
replace
swap1
swap2
swap
swap


Is this what you were expecting?
# 10  
Old 04-05-2006
hi matrixmadhan

thanks for your reply and time.
and thanks for your awk solution.

but one quetion
is it possible bu using sed?
if u refer man pages then its clearly mentioned -- by specifying flags we can replace nth instance.

so just see is it possible by using sed.

Thanks again
Regards,
swapneel
# 11  
Old 04-05-2006
sed's s command operates on the pattern space. By default sed will load one line at a time into the pattern space. So by specifying a flag "1", you ask sed the change the first occurence on each line. With complex sed scripts you can arrange for several lines to be in the pattern buffer at once. But if you try to load the entire file into the pattern buffer and then execute a single s command, you are limiting yourself to small files only. A general sed solution:

sed '-e/swap/{;s/swap/swap1/;:a' '-en;ba' '-e}' <filename
# 12  
Old 04-05-2006
hi Perderabo

ya its working fine. but frankly i m not able to understand
-en;ba parameters...

sed '-e/swap/{;s/swap/swap1/;:a' '-en;ba' '-e}' <filename

could you plz explain us its execution in short.
as i m struggling with this since long time.

thanks a lot
regards,
swapneel
# 13  
Old 04-05-2006
The /swap/{....} will run the program in the braces when a line with "swap" is encountered. The program replaces swap with swap1. Only the first instance on the line is replaced, no 1 flag is needed since that is the default. Then there is an infinite loop reading lines and printing them out. This uses up the rest of the file and sed will automatically terminate the infinite loop.
# 14  
Old 05-03-2009
I have a list of IPs, and I want to delete just '10.0.0.1'. I would like a sed/awk solution that will only delete '10.0.0.1' and not modify all the IPs
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
10.0.0.6
10.0.0.7
10.0.0.8
10.0.0.9
10.0.0.10
10.0.0.11
Tried:
Code:
sed -e '/10.0.0.1/d' file
sed '/10.0.0.1/d' file
sed 's/10.0.0.1//1' file

but they all modify the list by removing all the instances of 10.0.0.1 out of the list so I'm only left with
2
3
4
5
6
7
8
9
0
1
Any suggestions?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed substitue whole file + 1 substitue with variables on one sed line?.

I'm trying to remove '--X' from the whole file and using variables replace $oldvar with $newvar. I have tried with double quotes but it doesn't seem to work. $newvar is set to /usr/bin/bash. Would appreciate some guidance. newvar=$(which bash) oldvar=/bin/bash sed... (1 Reply)
Discussion started by: itman73
1 Replies

2. Shell Programming and Scripting

sed to grab first instance of a range

Hi all, I'm new to the forum and also relatively new to sed and other such wonderfully epic tools. I'm attempting to grab a section of text between two words, but it seems to match all instances of the range instead of stopping at just the first. This occurs when I use: sed -n... (7 Replies)
Discussion started by: Lazarix
7 Replies

3. Shell Programming and Scripting

sed for first instance of a pattern

Hi Everyone, I have the below information from a log file: LOAD SUMMARY ============ WRT_8036 Target: TGT_1_TAB (Instance Name: ) WRT_8039 Inserted rows - Requested: 3929 Applied: 0 Rejected: 3929 Affected: 0 Mutated from update: 3929 WRT_8041 Updated rows ... (7 Replies)
Discussion started by: galaxy_rocky
7 Replies

4. Shell Programming and Scripting

sed command to print first instance of pattern in range

The following text is in testFile.txt: one 5 two 10 three 15 four 20 five 25 six 10 seven 35 eight 10 nine 45 ten 50 I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency,... (9 Replies)
Discussion started by: uschaafm
9 Replies

5. Shell Programming and Scripting

Using sed can you specify the last instance of a character on a line?

I was told a way to do this with awk earlier today but is there a way with sed to specify the last instance of a character on a line? You will know what character you're looking for but there could be none or one hundred instances of it on a line say and you ONLY want to specify the last one for... (3 Replies)
Discussion started by: Bashingaway
3 Replies

6. Homework & Coursework Questions

Bash Scripting - sed (substitue)

1. The problem statement, all variables and given/known data: I have been asked to create a bash script to delete comments from another file but in the file they have an echo command with this inside of it /* this is an echo */\ so obviously they want to keep this one in the file. I have found... (5 Replies)
Discussion started by: syco__
5 Replies

7. Shell Programming and Scripting

sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this. sed '/^begpattern/,/endpattern/d' myfile However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed? Or can awk... (8 Replies)
Discussion started by: mariod1049
8 Replies

8. Shell Programming and Scripting

sed appending needed only after first instance

Hi, Here is my piece of code used with sed in shell script: sed -i '/<falsemodule-option>/ a\<LdapLogin>' myxmlfile The problem that i am facing with the above is that in 'myxml' file i have mulitple instances of <falsemodule-option> so when i execute the above sed command, it is appending... (10 Replies)
Discussion started by: sunrexstar
10 Replies

9. Shell Programming and Scripting

Sed on first instance only

Hi, I've been trying to figure this one out and found a post about this on the forum here but the solution didn't seem to work for me. Basically what I have is a file that looks something like: stuff morestuff 0 otherthing 0 etc I want to substitute for the 0 but what I want to... (9 Replies)
Discussion started by: eltinator
9 Replies

10. Shell Programming and Scripting

sed replace 2nd instance

Hello, I want to replace 2nd instance of "foo" in a file use sed. Any suggestions? (2 Replies)
Discussion started by: katrvu
2 Replies
Login or Register to Ask a Question