Interesting question on sed

This is a discussion on Interesting question on sed within the Linux Programming forums, part of the Platform Specific Boards category; Hi All, I have the following file. $ cat test #include "asd.h" #include "awr.h" #include "asfa.h" case 1: $ cat ...

  1. #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

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

  3. #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.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    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)
    Last edited by brewbuck; 10-28-2009 at 10:14 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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