I have a table and I want to add a column containing, for each line, the name of the file itself. I will post an example below: This is the input:

SPATA17 1   217947738
LYPLAL1 1   219383905
FAM47E  4   77192838
SHROOM3 4   77660162
SHROOM3 4   77660731
SHROOM3 4   77662248

And this the output:

SPATA17 1   217947738 file1
LYPLAL1 1   219383905 file1
FAM47E  4   77192838  file1
SHROOM3 4   77660162  file1
SHROOM3 4   77660731  file1
SHROOM3 4   77662248  file1

I applied the very useful solution you have proposed in this forum (the solution is the one using sed function: for f in file1 file2 file3; do sed -i "s/$/\t$f/" $f; done) but I have some troubles. The output is not as expected. This is how I visualize it with Vim

SPATA17 1   217947738^M file1
LYPLAL1 1   219383905^M file1
FAM47E  4   77192838^M  file1
SHROOM3 4   77660162^M  file1
SHROOM3 4   77660731^M  file1
SHROOM3 4   77662248^M  file1

Is this possibly an error because new line (\n) needs to be removed before? I found the solution not working for me.

  • 2
    Please edit your question and tell us exactly which "solution" you applied. Your link is pointing to your deleted post on that page so we don't know which of the answers you used. Also, if the original file was created in a non-*nix system (Windows, for example) you need to tell us. The ^M are \r, not \n and that suggests a Windows file.
    – terdon
    Jul 13, 2016 at 12:37
  • Now it should be correct. The file is created in Ubuntu. Jul 13, 2016 at 13:23
  • Well, it looks like whatever tool you used to create the original file added \r. You should probably report is as a bug.
    – terdon
    Jul 13, 2016 at 13:32

2 Answers 2


The ^M are \r, "carriage returns" and were, apparently, already there in your original file. You can remove them with:

sed -i 's/\r//' file1 file2 file3
  • @KristinaGagalova you're welcome. If one of the answers here solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.
    – terdon
    Jul 13, 2016 at 13:46

Apart from \r remove the loop can be subtitute by

sed -ni 'P;F' file1 file2 file3 …

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .