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!

-----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-----

The working directory is ouput to standard out by

pwd

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

Would something like


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 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-----

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.

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:


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

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:

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:

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:

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.

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.

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

Why don’t you give us an example of what you want OUT of sed as well as
what you believe you want in it. If you want to send in the directory
from ‘pwd’ and get out just ‘B’ (or whatever is on the end) that would
be good to know indeed.

Good luck.

Neophyte42 wrote:
> 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.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIr5DN3s42bA80+9kRAkgbAJwIB7XBmSTgUCNNZlbCh5dEd/aNQgCfYKVU
CoV4tHqQRZPhZcthvomgFzw=
=eN/Z
-----END PGP SIGNATURE-----

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

To get the last character on a path:

echo /path/to/something/great/A | sed “s/.*(.)/\1/”

Good luck.

ab@novell.com wrote:
> Why don’t you give us an example of what you want OUT of sed as well as
> what you believe you want in it. If you want to send in the directory
> from ‘pwd’ and get out just ‘B’ (or whatever is on the end) that would
> be good to know indeed.
>
> Good luck.
>
>
>
>
>
> Neophyte42 wrote:
>> 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.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIr5YI3s42bA80+9kRAnItAJ0ZzSe97yGAtJWWU5a3sBV5HA1/YACeLjYu
JHCDlnpDvZWay/KwptH8cgg=
=+buz
-----END PGP SIGNATURE-----

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

Oh neato… this one will get the last alphabetical character even if
you have a trailing slash:

echo /path/to/something/great/A | sed “s/.*?(\w)/?/\1/”

Good luck.

ab@novell.com wrote:
> To get the last character on a path:
>
> echo /path/to/something/great/A | sed “s/.*(.)/\1/”
>
> Good luck.
>
>
>
>
>
> ab@novell.com wrote:
>> Why don’t you give us an example of what you want OUT of sed as well as
>> what you believe you want in it. If you want to send in the directory
>> from ‘pwd’ and get out just ‘B’ (or whatever is on the end) that would
>> be good to know indeed.
>
>> Good luck.
>
>
>
>
>
>> Neophyte42 wrote:
>>> 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.
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIr5b/3s42bA80+9kRAlhFAJ9M1OvzHpbsi3PTAlXVV/2XFM7NUwCeJ2HU
rGpm/VIfXn86WNKs2k8NUZM=
=pZAZ
-----END PGP SIGNATURE-----

IMHO it is better to use $(…) than ..... because it is more readable (specialy when mixed with ’ and " in a statement) and it can be nested. I do not know why generations upon generations of shell programmers pass this ... contruction on to their heirs. The $(…) is there at least there since ksh.

Then cutting things of in the beginning:

MYPATH="$(pwd)"
MYDIR="${MYPATH##*/}"
sed s+A+${MYDIR}+ inputfile

You should realy try to read some sections of the man page of bash.
The motto is: First man/info, then Google, then post.

To ab@novell.com.

I am afraid you are still trying to give a solution to the problem as the OP worded it in the beginning: how to let **sed **work on the string containing the path where you are.

Now we found out that his question was: how do I put the path where I am into the **sed **statement I am composing (needless to day that it is unimported if it is a **sed **statement, it can be any statement).

Last developement is that he does not want the path, but only the last item of the path.

I hope you will take this post as it is intended: to spare you time on offering solutions to the wrong problem :wink:

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

But I can at least solve the wrong ones… if I actually try to fix the
right problems I’ll just fail and look dumb instead of succeeding at
nothing and looking dumb which is a slight improvement, right? Right?

I’ll learn to read someday and avoid all these awkward posts… thanks
for the note.

Good luck.

hcvv wrote:
> To ab@novell.com.
>
> I am afraid you are still trying to give a solution to the problem as
> the OP worded it in the beginning: how to let *sed *work on the string
> containing the path where you are.
>
> Now we found out that his question was: how do I put the path where I
> am into the *sed *statement I am composing (needless to day that it is
> unimported if it is a *sed *statement, it can be any statement).
>
> Last developement is that he does not want the path, but only the last
> item of the path.
>
> I hope you will take this post as it is intended: to spare you time on
> offering solutions to the wrong problem :wink:
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIr9gH3s42bA80+9kRAuuRAJ9nErC+R76skMUlzsiRWHmkAyE5ZACfeAlk
dnd07LwYxFRyMr6vDHc2zL8=
=5VW6
-----END PGP SIGNATURE-----

My conclusion from several of your posts in several threads: I have no doubt you will solve most problems, those asked and those not asked lol!

Try this.

pwd | sed ‘s,^(./)?(^/]),\2,’

I think I have figured it out.

Putting:


$(basename $(pwd))

Into the appropriate place in my sed command should work. I’ve tested this with echo and I get the section of the directory name that I want.

Thanks for the suggestions (both direct and the RTFM comments) and the help in getting me to properly formulate my question. The suggestion of using dirname led me to learn about basename. Learning that $(command) is equivalent to command and that this works the way I thought it did was helpful too.

I’ll reread the bash scripting section of my Sobell text.

Congratulations :shake:

I see that my story about the possibility to nest $(…) constructs is understood very well.

These sorts of things make it a joy for me to try to help other forum members!

And, to be nasty, you did find the ${MYPATH##/} construct to? It is almost the same as basename (in this case with ##/ ), but it does not involve the execution of the basename command (as it is all done inside the shell). On the other hand the $(basename $(pwd)) is compact and very readable/understandable.