Page 1 of 3 123 LastLast
Results 1 to 10 of 28

Thread: pass working directory name from bash to sed

  1. #1

    Default pass working directory name from bash to sed

    From a bash script, how can I pass the working directory name into sed as text?

    I ultimately want to have several directories A, B, C, ... that each have a file where line 10 is "A" after copying and pasting. I then want to run a script so that that line of the file in directory B is "B" and so on. I'm happy to consider a different appraoch if that would be better.

    Thanks!

  2. #2
    ab@novell.com NNTP User

    Default Re: pass working directory name from bash to sed

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Try the 'dirname' command as it may be what you need.

    Good luck.





    Neophyte42 wrote:
    > From a bash script, how can I pass the working directory name into sed
    > as text?
    >
    > I ultimately want to have several directories A, B, C, ... that each
    > have a file where line 10 is "A" after copying and pasting. I then want
    > to run a script so that that line of the file in directory B is "B" and
    > so on. I'm happy to consider a different appraoch if that would be
    > better.
    >
    > Thanks!
    >
    >

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFIrZqS3s42bA80+9kRAk0QAJ4xkpsBYZ2VAQnl6CRXZJ7qkSSjdwCfe5+d
    C8uNmrId6PAw9HVJj/qMu+4=
    =GPnv
    -----END PGP SIGNATURE-----

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,864

    Default Re: pass working directory name from bash to sed

    The working directory is ouput to standard out by
    Code:
    pwd
    Henk van Velden

  4. #4

    Default Re: pass working directory name from bash to sed

    Right, but how do I get std out into a sed command?

    Would something like
    Code:
    cd path/B
    sed 10/A/`pwd`/s file
    do what I want (and I know I need to review my sed - it was never that strong in the first place, so forgive some syntax - I'm away from my Linux machine or I'd check the manpage and/or my sed & awk O'Reilly book)?

    It is getting the directory name into my sed command that I don't understand. This isn't a simple pipe or redirect.

  5. #5
    ab@novell.com NNTP User

    Default Re: pass working directory name from bash to sed

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    set to a variable and use that in sed perhaps?

    mydir=`pwd`

    What exactly are you doing with sed again in the long run?

    Good luck.




    Neophyte42 wrote:
    > Right, but how do I get std out into a sed command?
    >
    > Would something like
    >
    > Code:
    > --------------------
    >
    > cd path/B
    > sed 10/A/`pwd`/s file
    >
    > --------------------
    >
    > do what I want (and I know I need to review my sed - it was never that
    > strong in the first place, so forgive some syntax - I'm away from my
    > Linux machine or I'd check the manpage and/or my sed & awk O'Reilly
    > book)?
    >
    > It is getting the directory name into my sed command that I don't
    > understand. This isn't a simple pipe or redirect.
    >
    >

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD4DBQFIrcfv3s42bA80+9kRAhOaAJdGTq/3B6RmlaL2+y8CZVBUgwQiAJ9Ip/N9
    UeDagy6bHX1ePIeFxRFEnw==
    =7Fsj
    -----END PGP SIGNATURE-----

  6. #6

    Default Re: pass working directory name from bash to sed

    Quote Originally Posted by ab@novell.com View Post
    -----BEGIN PGP set to a variable and use that in sed perhaps?

    mydir=`pwd`

    What exactly are you doing with sed again in the long run?

    Good luck.




    Neophyte42 wrote:
    > Right, but how do I get std out into a sed command?
    >
    > Would something like
    >
    > Code:
    > --------------------
    >
    > cd path/B
    > sed 10/A/`pwd`/s file
    >
    > --------------------
    >
    > do what I want (and I know I need to review my sed - it was never that
    > strong in the first place, so forgive some syntax - I'm away from my
    > Linux machine or I'd check the manpage and/or my sed & awk O'Reilly
    > book)?
    >
    > It is getting the directory name into my sed command that I don't
    > understand. This isn't a simple pipe or redirect.
    >
    >
    I have a file, X, in a directory, A. I then copy A multiple times so now I have directories A, B, C, ..., that each have file X. Right now, line 10 of file X in each directory is A (with a little leading white-space, but I can lose this if necessary).

    What I want is for line 10 of file X in directory B to be B instead of A and so on for each directory. Right now I'm doing this manually and would prefer to make a script of some sort. Passing the output of pwd into a sed expression somehow was my first idea, but I am open to others.

    I can't be the first person who has ever wanted to do something like this.

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,864

    Default Re: pass working directory name from bash to sed

    Sed is the stream editor, the stream is from standard-in to standard-out. So when you want to sed something that comes from standard-[out of one program you pipe it to standard-in of sed:
    pwd | sed ........
    This is so basic. Try first to read something about shell commands.

    And what Neophyte42 tells you does work also. But it depends a lot on what you have like: is there only one line of ttext with A in that file, when not can't there be by change a piece of text in the file that also contains an A, but that should not be changed, etc.
    Henk van Velden

  8. #8

    Default Re: pass working directory name from bash to sed

    Quote Originally Posted by hcvv View Post
    Sed is the stream editor, the stream is from standard-in to standard-out. So when you want to sed something that comes from standard-[out of one program you pipe it to standard-in of sed:
    pwd | sed ........
    This is so basic. Try first to read something about shell commands.

    And what Neophyte42 tells you does work also. But it depends a lot on what you have like: is there only one line of ttext with A in that file, when not can't there be by change a piece of text in the file that also contains an A, but that should not be changed, etc.
    First, I am the OP.

    I don't think simple piping or redirection will work for me because I want the data from pwd command to arrive in the middle of the sed arguments - I don't know of a way to control where within the /find_exp/replace_with_text/ string the redirected or piped output would go.

    But maybe I should build the sed command in bash, something like:

    Code:
    cd where_the_file_lives
    PWD_VAR=`pwd` //do i need the accent mark for this?
    SED_COM=10/A/$PWD_VAR/s //i might have line number and s switched positionally
    sed -options $SED_COM file

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,864

    Default Re: pass working directory name from bash to sed

    From a bash script, how can I pass the working directory name into sed as text?
    The above was the first question when you started the thread. Now the answer is:
    Code:
    pwd | sed ......
    Now your question changed to: How can I have the ouput of the pwd command somewhere in the the command line of another command (e.g. a sed command). The answer is:
    Code:
    command with parameter $(pwd) and other parameters or as part of a parameter lala$(pwd)lolo
    Where $(pwd) can also be written as `pwd` as is suggested earlier.

    When you find out hafway the duscussion that you did not formulate you question correct in the beginning, that is no problem. You reformulate your question and we will try to help you again, but it is still you who has to ask the right question to get the right answer, else GIGO.

    So to put the current directory somewhere in a sed command will look like:
    Code:
    sed s+A+$(pwd)+ inputfile
    I used + as the fieldboundaries instead of the customary / because pwd will most certainly insert several / in the string.

    When there are more A in any line of inputfile you can add g to the substitute string, etc. But I suppose you are familiar with reading the sed manpage.
    Henk van Velden

  10. #10

    Default Re: pass working directory name from bash to sed

    Ok, I worded my initial question poorly. Thanks for putting up with the evolution of my question.

    I think I am almost where I want to be, but I only want the name of the directory, not the path stuff that pwd returns. I read the man and info pages for pwd and did not see an option to turn off the path information.

    For example, if the directory is /home/Neophyte42/work/B I want to put "B" into my sed command and not "/home/Neophyte42/work/B".

    How can I do this?

    Also, thank you for confirming that `command` worked the way I thought it did - I don't know that I've ever used that before and am kind of surprised I remember it from C in Unix class.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •