I am able to prepend the filename to each line in a text file named test1 with this command:
nawk '{print FILENAME"\,"$0}' test1
I would like to prepend the full path of the file to each line of the file instead.
I am able to prepend the filename to each line in a text file named test1 with this command:
nawk '{print FILENAME"\,"$0}' test1
I would like to prepend the full path of the file to each line of the file instead.
Using readlink
should work for you.
More info here
To get the full path use:
readlink -f relative/path/to/file
If
readlink -f
is not available on your system you can use this:function myreadlink() { ( cd $(dirname $1) # or cd ${1%/*} echo $PWD/$(basename $1) # or echo $PWD/${1##*/} ) }
EDIT:
As mentioned in the comments, it would be a good idea to edit the myreadlink
function to use
pushd $(dirname $1) ... popd
to save the working directory and then restore it after execution.
Could you please try following and let me know if this helps you.
nawk -v PWD=`pwd` '{print PWD"\,"$0}' Input_file
In above command I am considering that your Input_file is present in current directory where you are executing this command, if this command is part of any script then you could set PWD variable with it's file name or even if you are doing cd to a path before this command into a script then also this should work.
Also since I don't have nawk in my system so fair warning if -v PWD
doesn't work please try to use -vPWD
. Hope this helps.
EDIT1: Adding filename too in solution as per OP's request.
nawk -v PWD=`pwd` '{print PWD"/"FILENAME","$0}' Input_file
PS: If you are trying to escape comma in above then you need NOT to put \,
in case you want to add \,
between lines and path of Input_file then put \\,
to escape \
here.