我有100个文本文件,如下所示:
File title 4 Realization number variable 2 name variable 3 name variable 4 name 1 3452 4538 325.5第7行(1)的第一个数字是实现号,它应该与文件名相关。 即第一个文件名为file1.txt,实现编号为1(如上所示)。 第二个文件名为file2.txt,第7行的实现编号为2。 file3.txt应该在第7行有第3个实现,依此类推......
不幸的是,每个文件都有实现= 1,它们应该根据文件名递增。
我想从每个文件的第7行(3452,4538和325.5)中提取变量2,3和4,并将它们附加到名为summary.txt的摘要文件中。
我知道如何从1个文件中提取信息:
awk 'NR==7,NR==7{print $2, $3, $4}' file1.txt哪个,正确地给了我:
3452 4538 325.5我的第一个问题是,当从多个文件上的bash脚本运行时,此命令似乎没有给出相同的结果。
#!/bin/bash for ((i=1;i<=100;i++));do awk 'NR=7,NR==7{print $2, $3, $4}' File$((i)).txt done当我使用上面的脚本时,我会在屏幕上打印多行。
其次,我想将这些值与CORRECT前面的实现编号一起输出到摘要文件中。 即我想要一个看起来像这样的文件:
1 3452 4538 325.5 2 4582 6853 158.2 ... 100 4865 3589 15.15谢谢你的帮助!
I have 100 text files which look like this:
File title 4 Realization number variable 2 name variable 3 name variable 4 name 1 3452 4538 325.5The first number on the 7th line (1) is the realization number, which SHOULD relate to the file name. i.e. The first file is called file1.txt and has realization number 1 (as shown above). The second file is called file2.txt and should have realization number 2 on the 7th line. file3.txt should have realization number 3 on the 7th line, and so on...
Unfortunately every file has realization=1, where they should be incremented according to the file name.
I want to extract variables 2, 3 and 4 from the 7th line (3452, 4538 and 325.5) in each of the files and append them to a summary file called summary.txt.
I know how to extract the information from 1 file:
awk 'NR==7,NR==7{print $2, $3, $4}' file1.txtWhich, correctly gives me:
3452 4538 325.5My first problem is that this command doesn't seem to give the same results when run from a bash script on multiple files.
#!/bin/bash for ((i=1;i<=100;i++));do awk 'NR=7,NR==7{print $2, $3, $4}' File$((i)).txt doneI get multiple lines being printed to the screen when I use the above script.
Secondly, I would like to output those values to the summary file along with the CORRECT preceeding realization number. i.e. I want a file that looks like this:
1 3452 4538 325.5 2 4582 6853 158.2 ... 100 4865 3589 15.15Thanks for any help!
最满意答案
您可以简化一些事情并获得您追求的结果:
#!/bin/bash for ((i=1;i<=100;i++)) do echo $i $(awk 'NR==7{print $2, $3, $4}' File$i.txt) done你真的不想分配到NR = 7(正如你所做的那样),你也不需要重复NR==7,NR==7 。 当$i足够时,你也真的不需要$((i))表示法。
如果所有文件都长度为7行,则可以在一个awk命令中完成所有操作(而不是100个):
awk 'NR%7==0 { print ++i, $2, $3, $4}' Files*.txtYou can simplify some things and get the result you're after:
#!/bin/bash for ((i=1;i<=100;i++)) do echo $i $(awk 'NR==7{print $2, $3, $4}' File$i.txt) doneYou really don't want to assign to NR=7 (as you did) and you don't need to repeat the NR==7,NR==7 either. You also really don't need the $((i)) notation when $i is sufficient.
If all the files are exactly 7 lines long, you can do it all in one awk command (instead of 100 of them):
awk 'NR%7==0 { print ++i, $2, $3, $4}' Files*.txt使用awk从文本文件中提取值(extracting values from text file using awk)我有100个文本文件,如下所示:
File title 4 Realization number variable 2 name variable 3 name variable 4 name 1 3452 4538 325.5第7行(1)的第一个数字是实现号,它应该与文件名相关。 即第一个文件名为file1.txt,实现编号为1(如上所示)。 第二个文件名为file2.txt,第7行的实现编号为2。 file3.txt应该在第7行有第3个实现,依此类推......
不幸的是,每个文件都有实现= 1,它们应该根据文件名递增。
我想从每个文件的第7行(3452,4538和325.5)中提取变量2,3和4,并将它们附加到名为summary.txt的摘要文件中。
我知道如何从1个文件中提取信息:
awk 'NR==7,NR==7{print $2, $3, $4}' file1.txt哪个,正确地给了我:
3452 4538 325.5我的第一个问题是,当从多个文件上的bash脚本运行时,此命令似乎没有给出相同的结果。
#!/bin/bash for ((i=1;i<=100;i++));do awk 'NR=7,NR==7{print $2, $3, $4}' File$((i)).txt done当我使用上面的脚本时,我会在屏幕上打印多行。
其次,我想将这些值与CORRECT前面的实现编号一起输出到摘要文件中。 即我想要一个看起来像这样的文件:
1 3452 4538 325.5 2 4582 6853 158.2 ... 100 4865 3589 15.15谢谢你的帮助!
I have 100 text files which look like this:
File title 4 Realization number variable 2 name variable 3 name variable 4 name 1 3452 4538 325.5The first number on the 7th line (1) is the realization number, which SHOULD relate to the file name. i.e. The first file is called file1.txt and has realization number 1 (as shown above). The second file is called file2.txt and should have realization number 2 on the 7th line. file3.txt should have realization number 3 on the 7th line, and so on...
Unfortunately every file has realization=1, where they should be incremented according to the file name.
I want to extract variables 2, 3 and 4 from the 7th line (3452, 4538 and 325.5) in each of the files and append them to a summary file called summary.txt.
I know how to extract the information from 1 file:
awk 'NR==7,NR==7{print $2, $3, $4}' file1.txtWhich, correctly gives me:
3452 4538 325.5My first problem is that this command doesn't seem to give the same results when run from a bash script on multiple files.
#!/bin/bash for ((i=1;i<=100;i++));do awk 'NR=7,NR==7{print $2, $3, $4}' File$((i)).txt doneI get multiple lines being printed to the screen when I use the above script.
Secondly, I would like to output those values to the summary file along with the CORRECT preceeding realization number. i.e. I want a file that looks like this:
1 3452 4538 325.5 2 4582 6853 158.2 ... 100 4865 3589 15.15Thanks for any help!
最满意答案
您可以简化一些事情并获得您追求的结果:
#!/bin/bash for ((i=1;i<=100;i++)) do echo $i $(awk 'NR==7{print $2, $3, $4}' File$i.txt) done你真的不想分配到NR = 7(正如你所做的那样),你也不需要重复NR==7,NR==7 。 当$i足够时,你也真的不需要$((i))表示法。
如果所有文件都长度为7行,则可以在一个awk命令中完成所有操作(而不是100个):
awk 'NR%7==0 { print ++i, $2, $3, $4}' Files*.txtYou can simplify some things and get the result you're after:
#!/bin/bash for ((i=1;i<=100;i++)) do echo $i $(awk 'NR==7{print $2, $3, $4}' File$i.txt) doneYou really don't want to assign to NR=7 (as you did) and you don't need to repeat the NR==7,NR==7 either. You also really don't need the $((i)) notation when $i is sufficient.
If all the files are exactly 7 lines long, you can do it all in one awk command (instead of 100 of them):
awk 'NR%7==0 { print ++i, $2, $3, $4}' Files*.txt
发布评论