I am trying to add filenames to many *txt files, which are tab-seperated (they will eventually be joined together using cat) I am using bash, on Ubuntu 14.04.

So far my code is;

for f in *.txt
sed -i 's/$/'"       $f"'/' "$f"

This works fine except that the filenames are appended on the line below, and not to each row. I am using Ctrl-V Tab to insert my tab in the console.


You can try this:

for f in *.txt
  sed -i -e '1,$s/$/'"       $f"'/' "$f"

if you are trying to do what I think you do: add a {tab} character and file name at the end of each line in all the .txt files in a directory

  • also instead of using ctrl-v tab combo to insert the tab character, you can use \t string to insert a tab in sed. So your sed line should look like this sed -i -e '1,$s/$/'"\t$f"'/' $f – MelBurslan Jan 27 '16 at 0:03
  • Upvoted! But don' have enough reputation... – danny_C_O_T_W Jan 27 '16 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.