I'm sure there are other ways to do it, I'd use perl.
Up vote 0 down vote favorite 3 share g+ share fb share tw.
I need to do the following for hundreds of files: Append the name of the file (which may contain spaces) to the end of each line in the file. It seems to me there should 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 a different solution using bash, awk, etc.? perl bash sed awk link|improve this question edited Nov 21 '08 at 19:46Leon Timmermans18.5k12975 asked Nov 21 '08 at 17:48MCS1,87562044 85% accept rate.
I'm sure there are other ways to do it, I'd use perl: perl -p -i -e 's/$/$ARGV.
Nice! Though it only worked if I separated the options and did perl -pi -e instead of perl -pie. – MCS Nov 21 '08 at 18:14 @MCS - thanks for the heads-up; I fixed the code sample.
– shmuelp Nov 21 '08 at 18:33 -pie won't work, because -i takes an optional argument. -pei or -epi should work OTOH. – Leon Timmermans Nov 21 '08 at 20:29 @LeonTimmermans, -pei and -epi won't work because -e also expects an argument.
You can't really bundle -i and -e into the same group. – Ven'Tatsu Nov 21 '08 at 16:19 @Ven'Tatsu: yeah, that's a stupid mistake on my part – Leon Timmermans Nov 21 '08 at 8:41.
Some versions of sed support the "--in-place" argument so you can condense Tyler's solution to for I in * ; do sed -e "s/\$/$i/" --in-place "$i" done.
Good suggestion. I'm not a sed expert as you can see. :) However you do need a linebreak (or a semi-colon) between the * and the do.
– Tyler McHenry Nov 21 '08 at 18:02 And of course when I typed it into a bash shell to test it, I had the semi-colon. – Paul Tomblin Nov 21 '08 at 18:06.
You could do it with a bash script for I in * do sed -e "s/\$/$i/" "$i" done One-liner version: for I in * ; do sed -e "s/\$/$i/" "$i" ; done Edit: If you want to replace the contents of the file with the new, name-appended lines, do this: TFILE=`mktemp` for I in * do sed -e "s/\$/$i/" "$i" > $TFILE cp -f $TFILE "$i" done rm -f $TFILE.
You need quotes around all filename variables for this to work with files that contains spaces as the OP required. – Robert Gamble Nov 21 '08 at 18:01 For things like this, rather than using mktemp, I'll use "TFILE=tmp. $$".
"$$" is the PID, so it's going to be unique. Just a stylistic difference. – Paul Tomblin Nov 21 '08 at 18:14 Just use the -i option to sed to edit in place.
– Steve Baker Nov 21 '08 at 18:22 -i is a feature of gnu sed – hop Nov 21 '08 at 18:42.
More or less how Tyler suggested, just with some modifications to allow for spaces in the name. I was hoping for a one-liner though... ( OLDIFS=$IFS IFS=$'\n' for f in * do IFS=OLDIFS sed -e "s/\$/$f/" $f > tmpfile mv tmpfile $f IFS=$'\n' done ).
If you're willing to use perl, my answer above hits it in 25 characters. – shmuelp Nov 21 '08 at 18:16 Don't mess with IFS: use "$f" (in double quotes) for each reference. – Jonathan Leffler Nov 21 '08 at 18:19.
This might work for you: printf "%s\n" * | sed 's/. */sed -i "s|$| &|" &/' | bash.
In BASH, I'd do something to the effect of: for f in *; do echo $f >> $f; done.
That will append the name of the file to the end of the file, but not to the end of each line. – Tyler McHenry Nov 21 '08 at 17:56 I think that only adds the filename to the end of the file, not each line? – warren Nov 21 '08 at 17:56 OK, terribly sorry.
Misread the question. – ayaz Nov 21 '08 at 17:57.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.