LinuxQuestions.org
Review your favorite Linux distribution.
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 10-30-2014, 10:03 PM   #1
rookee
LQ Newbie
 
Registered: Jun 2014
Posts: 28

Rep: Reputation: Disabled
can someone help me understand this command and how it works please.


Bob@localhost [/home/Bob]
$ cat phone.list
Smith, Terry 7-7989
Adams, Fran 2-3876
StClair, Pat 4-6122
Brown, Robin 1-3745
Stair, Chris 5-5972
Benson, Sam 4-5587
Bob@localhost [/home/Bob]
$ cp phone.list phone.list2

Bob@localhost [/home/Bob]
$ sed '/Brown/s//Browne/' phone.list2 >> phone.list2

Bob@localhost [/home/Bob]
$ cat phone.list2
Smith, Terry 7-7989
Adams, Fran 2-3876
StClair, Pat 4-6122
Brown, Robin 1-3745
Stair, Chris 5-5972
Benson, Sam 4-5587

Smith, Terry 7-7989
Adams, Fran 2-3876
StClair, Pat 4-6122
Browne, Robin 1-3745
Stair, Chris 5-5972
Benson, Sam 4-5587
Bob@localhost [/home/Bob]
$ cp phone.list phone.list3

Bob@localhost [/home/Bob]
$ sed '/Brown/s//Browne/' phone.list3 > phone.list3

Bob@localhost [/home/Bob]
$ cat phone.list3

Bob@localhost [/home/Bob]



Can someone please help me understand how this works.

When a double redirection is given in the command it displays <content> along with the updated content appended to it. It is mentioned in the book(Unix Applications Programming Mastering the Shell by Ray Swartz) that the shell always scans a command line for shell meta characters before creating a process to execute the command. As a result (when single redirection is used), the shell sets up the redirection, which erases the contents of phone.list, before sed can get a chance to look at it.

With this mentioned in the book, when we give double redirection ">>" wouldn't the shell first scan the file phone.list2 and then perform the sed operation. If it does so then shouldn't we be getting a different output.

Please explain. Thanks in advance.
 
Old 10-30-2014, 10:42 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,696

Rep: Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261Reputation: 1261
All the ">>" redirection does is open the file at the end of the file, any output will then be appended to the end.

It is asking for trouble (as in doing this is an error) to try to process that file AND append to the end in the same command sequence. If you have a large enough file (due to buffering issues), the new data being appended will be read to be processed - thus forming an infinite loop, and a constantly growing file.

It isn't consistent - because if the file is small enough to fit in one buffer then the sed command could fully process the file (generate the output), and start exiting (which flushes the output file) before the new data shows up.
 
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
[SOLVED] Trying to understand how Mutt and Crontab works hahnhahnhahn Linux - General 2 01-08-2014 11:41 PM
Trying to understand how Ethernet works resetreset Linux - Networking 7 08-10-2012 01:33 PM
Do you understand how autotools works? hydraMax Programming 16 03-20-2012 05:19 PM
[SOLVED] understand how traffic control tc works ano Linux - Kernel 2 01-18-2011 05:55 AM
Does anyone understand why the audio works, but will not come through my TV? maestro52 Fedora 1 12-15-2009 07:00 PM


All times are GMT -5. The time now is 12:35 PM.

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