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. |
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. |
All times are GMT -5. The time now is 06:16 PM. |