Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop that prepends the filename to the beginning of each line in a tab separated file (thus generating a new column))

Tags:

linux

bash

sed

awk

I'm pretty new at this and try to loop over around 19000 files (all in one dir) in order to insert at the beginning of each line in each file the corresponding filename.

I will later concatenate these files to get a file containing all the data i need for my analyses.

File names show the following pattern:

V_foo_V_bar The two V_ parts in each filename are constant. the rest varies.

I tried:

for f in V*; do awk '{print "$f" $} file 

I did this with and without parentheses around $f and both didn't work.

With parentheses the string $f is inserted to the file name Without paretheses nothing is inserted

Help would be very much appreciated since I already tried a couple of other equally futile ideas of mine and frustration knocking on my door

Thanks a lot.

like image 329
pidoretroma Avatar asked Feb 21 '14 19:02

pidoretroma


2 Answers

To insert filename at the begining of each line of each file, try something like:

awk '{print FILENAME, $0}' V*

FILENAME is a built-in variable that carries the name of the file. V* will glob to all the files in your directory.

If you wish to put a separator between the name and lines you can do:

awk '{print FILENAME " : " $0}' V*
like image 81
jaypal singh Avatar answered Oct 19 '22 07:10

jaypal singh


Try:

for f in V*; do sed -i "s/^/$f\t/" "$f"; done

This assumes that no filenames have awkward characters such as *. Spaces should be ok, though.

like image 2
Joseph Quinsey Avatar answered Oct 19 '22 09:10

Joseph Quinsey