How do I add the filename to the end of each line in this file?
I need to do the following for hundreds of files: Add the filename (which may contain spaces) to the end of each line in the file.
I think there must be some way to do this:
sed -e 's/$/FILENAME/' *
where FILENAME
represents the name of the current file. Is there a sed variable representing the current filename? Or does anyone have another solution using bash, awk, etc.?
0
source to share
7 replies
You can do it with a bash script
for i in *
do
sed -e "s/\$/$i/" "$i"
done
Single layer version:
for i in * ; do sed -e "s/\$/$i/" "$i" ; done
Edit: If you want to replace the contents of the file with new lines with the appended name, follow these steps:
TFILE=`mktemp`
for i in *
do
sed -e "s/\$/$i/" "$i" > $TFILE
cp -f $TFILE "$i"
done
rm -f $TFILE
+4
source to share