LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 08-02-2012, 08:40 AM   #1
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 470

Rep: Reputation: Disabled
sed syntax


I found an old Oreilly book on sed/awk and was reading about it. It's syntax seems to deviate from what works present day.

The book states that something like this:

Code:
sed -e '/:/s/3/4/g' <./thing>./thing2
would go through every line but only change 3 to 4 on the first line which contains a colon.

It also states that putting a g in front:

Code:
sed -e 'g/:/s/3/4/g' <./thing>./thing2
should make it global. For me it seems to be global by default. the second sed syntax gives me an error.

I believe this book refers to the original unix sed, where I am using gnu sed. Is this someone that was changed in gnu sed or am I doing something wrong?
 
Old 08-02-2012, 09:10 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Let's break it down one step at a time.

/:/ -- This address matches every line that has a colon in it, not just the first.
If you don't supply an address, it defaults to every line of the input. If you supply two (add1,add2), then it matches every set of lines starting and ending with those addresses.

s/3/4/ -- This substitution command, without modifers, replaces the first 3 on the line matched with a 4.

g -- (only at the end of s///g) This modifies the substitution command to replace all 3's on the line with 4's. It's not file global, it's line global.

So the command 'sed /:/ s/3/4/g' changes all 3's to 4's on lines that contain colons.

The first 'g' on your line is where the error lies. 'g' is not a stand-alone command in sed. It's only valid [in the above sense] in ed or vi/vim, where it does mean "global", i.e. apply it to all lines. In sed you simply don't supply any addresses, as I mentioned.

Edit: Correction... 'g' is a valid command, but it doesn't mean global. In sed, it copies values from the hold space back to the pattern space, and is generally used when creating multi-line commands. The grymoire tutorial goes into this stuff in more detail:

http://www.grymoire.com/Unix/Sed.html

Last edited by David the H.; 08-02-2012 at 09:25 AM. Reason: as stated
 
Old 08-02-2012, 09:34 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To clarify a bit more, ed and vi are full text editors with random-access ability, and you need to be able to clearly tell all commands where to apply their modifications. Therefore the 'g' is used to apply them globally to all lines in the file.

sed is "stream" editor. This is important, because even though it takes it's syntax from ed/vi, it's designed to work on text in only one direction. The input flows into it from first line to last line, and sed always processes them in that order. The addresses are used to match which commands are used on which lines, and a "global" modifier isn't necessary. The 'g' has thus been re-purposed to work with the hold space instead (an extra buffer space that allows you to store text for later use).
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 command syntax incorrect acconboy Linux - Newbie 3 04-06-2012 02:56 PM
[SOLVED] sed between command syntax tnrooster Linux - Software 9 01-31-2011 09:08 PM
[SOLVED] Question about sed syntax in bash script musonio Linux - Software 2 08-21-2009 08:17 AM
Another SED Syntax Question... stlouis Programming 3 01-17-2008 09:50 PM
SED in BASH Script - Syntax Problem stlouis Programming 4 01-13-2008 11:38 AM


All times are GMT -5. The time now is 05:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration