LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2016, 05:50 AM   #1
arun natarajan
Member
 
Registered: Jun 2014
Posts: 111

Rep: Reputation: Disabled
sed 2nd occurance is not changing


Hi,

In sed, am not able to replace the 2nd occurrence as expected. Why ??
[root@server awk]# cat marks
1) Amit Physics 80
2) Rahul Physics 80
3) Shyam 87
4) Kedar English 85

[root@server awk]# sed -e 's/80/90/2' marks
1) Amit Physics 80
2) Rahul Physics 80
3) Shyam 87
4) Kedar English 85

Also, in sed, when not using the option 'g', hope it should replace only in 1st occurrence, but for me its changing in all occurrence. Why ??

[root@server awk]# sed -e 's/80/100/' marks
1) Amit Physics 100
2) Rahul Physics 100
3) Shyam 87
4) Kedar English 85


Waiting for solution for both queries.
 
Old 04-16-2016, 06:05 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
sed works on records. The number refers to the "nth" occurrence in each (matching) record. The "g" is a global change in each (matching) record.
 
Old 04-16-2016, 10:49 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You seem to be thinking of the second occurrence based on each line being an occurrence, but sed sees it as the number of occurrences on a single line:
Code:
sed -e 's/80/90/2' <<<'1) Amit 80 Physics 80'
1) Amit 80 Physics 90
 
Old 04-16-2016, 10:53 AM   #4
arun natarajan
Member
 
Registered: Jun 2014
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thanks grail, it worked as u said.

Last edited by arun natarajan; 04-16-2016 at 10:55 AM.
 
Old 04-16-2016, 10:56 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by arun natarajan View Post
Also, in sed, when not using the option 'g', hope it should replace only in 1st occurrence, but for me its changing in all occurrence. Why ??

[root@server awk]# sed -e 's/80/100/' marks
1) Amit Physics 100
2) Rahul Physics 100
3) Shyam 87
4) Kedar English 85


Waiting for solution for both queries.
If you want to stop after the FIRST change. Use a solution similar to this:
Code:
echo "foo bar
foo bar
foo bar" | sed '0,/bar/{s/bar/NAPALM DEATH/}'
foo NAPALM DEATH
foo bar
foo bar
As you can see, only the first 'bar' is changed. Only works with GNU sed which I assume you have.

Best regards,
HMW
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed: search for string, then match and replace the next occurance of a word nbritton Programming 4 01-21-2015 12:40 PM
Split string into 2, from 2nd occurance of a character in bash shridhar22 Linux - Newbie 9 07-24-2014 07:19 AM
Replace 2nd occurance of a special character after nth occurance of a delimiter from dhiru_b25@rediffmail.com Programming 6 11-01-2013 11:27 PM
sed usage(only repace one occurance) feetyouwell Linux - Software 7 01-31-2005 02:25 PM
sed usage(only repace one occurance) feetyouwell Linux - Software 2 01-31-2005 04:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration