Hello,
Is possibly there a way to prepend the filename to its content without a third file? The reason is to add a header to each file contents to distinguish each other when they are pasted side-by-side.
for i in *.txt; do (echo "geneID $i" ; cat $i) > ${i}; done
but got error: input file is output file
I'm aware of the solution is to use a tmp file, and rename it to the original name.
for i in *.txt; do (echo "geneID $i" ; cat $i) > ${i}.tmp; mv ${i}.tmp ${i}; done
I am wondering if there is a way to do this job without the "tmp" file. The closest function I can think of is to add filename to its content within vim and :wq which does not require a third file name, but I have hundreds of files to modify.
Thanks!
Thanks Don!
Now I understand xbin's way using the old editor ed.
The left side of the pipe bugs me with a mixture of printf "%s\n" and other options for ed, as I can only understand this way:
printf "%b\n" "1i\ngeneID\t$file\n.\nw" | ed -s $file
which seems working too.
When I learned vim, sometime I wished there is similar function as piped to receive stdout.
EDIT: I am so naive! Experts have dug way deeper than what I thought, like my this question. ed, ex, and new vipe in moreutils package.