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.
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*
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With