A loop that adds the file name to the beginning of each line in a file divided by a tab (this creates a new column))

I am new to this and trying to iterate over 19,000 files (all in one directory) to insert the corresponding file name at the beginning of each line in each file.

Later, I will combine these files to get a file containing all the data that I need for my analyzes.

The following pattern is displayed in the file names:

V_foo_V_bar The two parts V_in each file name are permanent. the rest varies.

I tried:

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

I did this with parentheses and without them around $f, and both of them did not work.

Using parentheses, a string is $finserted into the file name. No pastes are inserted.

Help would be greatly appreciated as I already tried a couple of other equally useless ideas and frustrations knocking on my door.

Many thanks.

+4
source share
3 answers

To insert a file name at the beginning of each line of each file, try something like:

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

FILENAMEis a built-in variable that carries a file name. V*there will be glob for all files in your directory.

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

awk '{print FILENAME " : " $0}' V*
+8
source

Try:

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

This assumes that the file names do not have uncomfortable characters, such as *. Spaces should be in order.

+2
source

Jaypal, , . ,

awk '{print FILENAME " : " $0}' V* >> all.csv
0
source

Source: https://habr.com/ru/post/1528182/


All Articles