In simple terms, there are file1.txt, file2.txt, file3.txt
file1.txt as follows:
1 a 4
d d g
file2.txt as follows:
2 b g
6 9 0
file3.txt as follows:
g h g
0 8 h
I now want the second column of the three files and their corresponding file name are printed out, and append to a file named all.txt
all.txt contents are as follows:
a file1.txt
d file1.txt
b file2.txt
9 file2.txt
h file3.txt
8 file3.txt
How to achieve the above demand it, here are two methods.
The first, is to add FILENAME way.
such as.
for i in *.txt
do
echo $i
awk ' print $2,FILENAME}' >> all.txt
done
Second. It is without FILENAME way.
for i in *.txt
do
echo $i
awk ' print $2,"'"${i}"'"}' >> all.txt
done