C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-28-2009, 05:00 AM   #1
Registered User
 
Join Date: Aug 2009
Posts: 11
Question Interesting question on sed

Hi All,

I have the following file.
$ cat test
#include "asd.h"
#include "awr.h"
#include "asfa.h"

case 1:

$ cat test | sed -e 's/i/I/g' -e 's/n/N/g/ >test

$cat test
#INclude "asd.h"
#INclude "awr.h"
#INclude "asfa.h"

case 2:

$ cat test | sed -e 's!\(#include\)[[:space:]]*\(.*\)!\1 \2!' -e 's!#include\ \"asd.h\"!#include\ \"ded.h\"!' >test

$ cat test
$

In case 2 the file "test" is blank.

Can anyone tell me why the file is balnked out in case-2 whereas it is not in case1?

I am using FreeBSD 6.3 and using BASH shell

Thanks,
14341
14341 is offline   Reply With Quote
Old 10-28-2009, 08:04 AM   #2
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
I would guess that it has something to do with your parans, but I don't know off the top of my head and I'd have to test what you've done to figure out what it is actually doing. What are you attempting to do? If you give us that, then we can probably help you get the correct sed script.
Kennedy is offline   Reply With Quote
Old 10-28-2009, 09:07 AM   #3
Registered User
 
Join Date: Aug 2009
Posts: 11
Quote:
Originally Posted by Kennedy View Post
I would guess that it has something to do with your parans, but I don't know off the top of my head and I'd have to test what you've done to figure out what it is actually doing. What are you attempting to do? If you give us that, then we can probably help you get the correct sed script.
I need to write a shell script that automatically changes the header file in a given list of files.

I will be given a list of files containing "from.h"

They will provide another header file say "to.h"

Now i need to replace *#include "from.h"* with *#include "to.h"* in the list of input files.
14341 is offline   Reply With Quote
Old 10-28-2009, 09:12 AM   #4
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 801
Hmm, sounds like homework to me. You want to do this in a shell script. Take the script and make sure you know how to parse the command line. I would create a usage() that told the user to use something like OLD=bla.h NEW=cool.h or the like. After that, all other strings on the command line are files that need to be updated. It is a simple for loop to do this.

Post your code (when you get some) and we'll help you the rest of the way.
Kennedy is offline   Reply With Quote
Old 10-28-2009, 10:08 AM   #5
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Your script works fine. It's the directing the output to the same file it came from which destroys the result. Don't do that.

And your first example does not work. It should be this:

Code:
cat test | sed -e 's/i/I/g' -e 's/n/N/g' > test
And if you try that, you see that it destroys the input as well. Directing out to the same file as the input is wrong.

(The reason why is pretty obvious. Shell pipelines are launched from right to left. The first thing that happens is all the fifos are created. Then sed gets launched, with output directed to 'test'. 'test' is opened in O_WRONLY mode, which destroys it. By the time cat starts up, the input file is already gone)

EDIT: Even if you remove the completely extraneous cat ( cat x | sed -e r can always be replaced with sed -e r x ) it still kills the input, again, because the shell opens the redirect before doing anything else)
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot

Last edited by brewbuck; 10-28-2009 at 10:14 AM.
brewbuck is offline   Reply With Quote
Old 10-29-2009, 12:31 AM   #6
Registered User
 
Join Date: Aug 2009
Posts: 11
Quote:
Originally Posted by brewbuck View Post
Your script works fine. It's the directing the output to the same file it came from which destroys the result. Don't do that.

And your first example does not work. It should be this:

Code:
cat test | sed -e 's/i/I/g' -e 's/n/N/g' > test
And if you try that, you see that it destroys the input as well. Directing out to the same file as the input is wrong.

(The reason why is pretty obvious. Shell pipelines are launched from right to left. The first thing that happens is all the fifos are created. Then sed gets launched, with output directed to 'test'. 'test' is opened in O_WRONLY mode, which destroys it. By the time cat starts up, the input file is already gone)

EDIT: Even if you remove the completely extraneous cat ( cat x | sed -e r can always be replaced with sed -e r x ) it still kills the input, again, because the shell opens the redirect before doing anything else)
Thank you for the reply........

I once again checked the two cases and i see that the result is inconsistent. Sometimes the output file is not blank and output's the result after sed's substitution.

i donno that shell open's the redirect first before processing.

Thank you,
14341
14341 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
another do while question kbpsu C++ Programming 3 03-23-2009 12:14 PM
A question about an interesting Makefile meili100 Tech Board 2 08-12-2008 03:56 PM
interesting question about new and nothrow new George2 C++ Programming 4 01-29-2008 03:53 AM
Design layer question mdoland C# Programming 0 10-19-2007 04:22 AM
Question type program for beginners Kirdra C++ Programming 7 09-15-2002 05:10 AM


All times are GMT -6. The time now is 10:44 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22