![]() |
| | #1 |
| Registered User Join Date: Aug 2009
Posts: 11
| 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 | |
| | #2 |
| Jaxom's & Imriel's Dad 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 | |
| | #3 | |
| Registered User Join Date: Aug 2009
Posts: 11
| Quote:
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 | |
| | #4 |
| Jaxom's & Imriel's Dad 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 | |
| | #5 |
| Senior software engineer 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 (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 | |
| | #6 | |
| Registered User Join Date: Aug 2009
Posts: 11
| Quote:
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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |