Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

sed command - substitue first instance

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 04-05-2006   -   Original Discussion by d_swapneel14
d_swapneel14's Unix or Linux Image
d_swapneel14 d_swapneel14 is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 13 October 2006, 11:13 AM EDT
Location: Mumbai,India
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #9  
Old Unix and Linux 04-05-2006   -   Original Discussion by d_swapneel14
matrixmadhan's Unix or Linux Image
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 10 January 2017, 8:10 AM EST
Location: classification algos
Posts: 3,215
Thanks: 19
Thanked 31 Times in 28 Posts
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?
Sponsored Links
    #10  
Old Unix and Linux 04-05-2006   -   Original Discussion by d_swapneel14
d_swapneel14's Unix or Linux Image
d_swapneel14 d_swapneel14 is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 13 October 2006, 11:13 AM EDT
Location: Mumbai,India
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
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 Unix and Linux 04-05-2006   -   Original Discussion by d_swapneel14
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 471 Times in 271 Posts
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
Sponsored Links
    #12  
Old Unix and Linux 04-05-2006   -   Original Discussion by d_swapneel14
d_swapneel14's Unix or Linux Image
d_swapneel14 d_swapneel14 is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 13 October 2006, 11:13 AM EDT
Location: Mumbai,India
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #13  
Old Unix and Linux 04-05-2006   -   Original Discussion by d_swapneel14
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Advisor  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 February 2016, 12:31 PM EST
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 471 Times in 271 Posts
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.
Sponsored Links
    #14  
Old Unix and Linux 05-03-2009   -   Original Discussion by d_swapneel14
mitch's Unix or Linux Image
mitch mitch is offline
Registered User
 
Join Date: Nov 2003
Last Activity: 2 December 2009, 8:17 PM EST
Location: Currently Kuwait/Iraq
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
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?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how do i access db2 instance via telnet command prompt solo123 Shell Programming and Scripting 4 09-07-2010 01:39 AM
substitue of values. rollthecoin Shell Programming and Scripting 2 08-17-2008 01:39 PM
Need to substitue space with \n krishmaths Shell Programming and Scripting 6 03-27-2008 07:39 AM
replace first instance(not first instance in line) IronHorse7 Shell Programming and Scripting 3 02-07-2008 01:29 PM
Need help for VNS substitue solution..... mehtasa UNIX for Advanced & Expert Users 0 01-18-2006 05:52 PM



All times are GMT -4. The time now is 11:55 AM.