I have a file with list of directories:
$ cat dirs.txt
/some/path
/other/path
/some/another/one
Current directory is /home/loom
$ echo $PWD
/home/loom
I would like to add $PWD
to each row of dirs.txt
like the following:
/home/loom/some/path
/home/loom/other/path
/home/loom/some/another/one
I unsuccessfully tried the command:
$ cat dirs.txt | awk '{print $PWD$1}'
/some/path/some/path
/other/path/other/path
/some/another/one/some/another/one
It just doubled each row. What command solves my problem?
I mean parametric answer, not cat dirs.txt | awk '{print "/home/loom" $1}'
ENVIRON
. e.g.awk '{print ENVIRON["PWD"]$1}' dirs.txt
. Using-v
makes for more readable code, IMO. Also watch out for filenames containing the=
character: awk will interpret them as variable assignments similar to-v
(but this happens after any BEGIN block). e.g.echo | awk '{print foo}' foo=bar
– cas Aug 16 '19 at 02:15-v
andENVIRON
is that the former will expand escape sequences (sofoo\tbar
will becomefoo<tab>bar
) while the latter won't. You can use ENVIRON for non-exported variables too, you just need to set the shell variable to itself on the awk command line, e.g.shellvar="$shellvar" awk 'BEGIN { awkvar=ENVIRON["shellvar"]...
. The issue about=
(or-
) in file names only applies to arguments, not to input. – Ed Morton Aug 16 '19 at 13:01