英文:
How do I print every nth entry of the mth column, starting from a particular line of a file?
问题
考虑文件file.txt中的以下数据:
$
$
$
FORCE 10 30 40
* 1 5 4
FORCE 11 20 22
* 2 3 0
FORCE 19 25 10
* 16 12 8
.
.
.
我想打印从第4行开始的第三列的每个第2个元素,结果如下:
30
20
25
我尝试过:
`cat file.txt | sed 's/\|/ /' | awk 'NR%2==4 {print $3}'`
但是,这没有打印任何内容,也没有生成任何错误。
英文:
Consider the following data in a file file.txt:
$
$
$
FORCE 10 30 40
* 1 5 4
FORCE 11 20 22
* 2 3 0
FORCE 19 25 10
* 16 12 8
.
.
.
I want to print every 2nd element of the third column, starting from line 4, resulting in:
30
20
25
I have tried:
cat file.txt | sed 's/\|/ /' | awk 'NR%2==4 {print $3}'
However, this is not resulting in anything being printed and no errors generated either.
答案1
得分: 3
你可以使用 awk
检查行号是否大于 3,然后检查偶数行号是否满足条件 NR%2==0
。
注意,你不需要使用 cat
。
print $3
}' file.txt
输出
30
20
25
英文:
You might use awk
checking that the row number > 3 and then check for an even row number with NR%2==0
.
Note that you don't have to use cat
awk 'NR > 3 && NR%2==0 {
print $3
}' file.txt
Output
30
20
25
答案2
得分: 1
使用 sed
$ sed -En '4~2s/([^ \t]*[ \t]+){2}([^ \t]*).*//p' input_file
30
20
25
英文:
Using sed
$ sed -En '4~2s/([^ \t]*[ \t]+){2}([^ \t]*).*//p' input_file
30
20
25
答案3
得分: 1
你不需要在使用GNU sed
时使用 cat
,因为它可以自己读取文件,此时应该是 sed 's/\|/ /' file.txt
。
你应该考虑是否需要那部分内容,你的示例输入根本没有管道字符,所以它对它不会产生任何影响。如果包含要打印的值的行没有该字符,也可以删除该部分。
输出为空是因为 NR%2==4
永远不成立,除法的余数始终小于除数 x(特别是在 %2
的情况下,只有两个可能的值:0
和 1
)。
英文:
> I have tried:
>
> cat file.txt | sed 's/\|/ /' | awk 'NR%2==4 {print $3}'
>
> However, this is not resulting in anything being printed and no errors
> generated either.
You do not need cat
whilst using GNU sed
as it can read file on its' own, in this case it would be sed 's/\|/ /' file.txt
.
You should consider if you need that part at all, your sample input does not have pipe character at all, so it would do nothing to it. You might also drop that part if lines holding values you want to print do not have that character.
Output is empty as NR%2==4
does never hold, remainder of division by x is always smaller than x (in particular case of %2
only 2 values are possible: 0
and 1
)
答案4
得分: 1
这对你可能有用(GNU sed):
sed -nE '4~2s/^((\S+)\s*){3}.*//p' file
通过设置-n
选项来关闭隐式打印,并通过打开-E
选项减少正则表达式中的反斜杠。
从第四行开始,然后每隔两行,捕获第三列并打印出来。
注意:\2
表示该反向引用的最后一个元素,与{3}
一起表示上面的内容。
替代方法:
sed -n '4,${s/^\(\(\S\+\)\s*\)\{3\}.*//p;n}' file
英文:
This might work for you (GNU sed):
sed -nE '4~2s/^((\S+)\s*){3}.*//p' file
Turn off implicit printing by setting the -n
option and reduce back slashes in regexps by turning on -E
.
From the fourth line and then every second line thereafter, capture the third column and print it.
N.B. The \2
represents the last inhabitant of that back reference which in conjunction with the {3}
means the above.
Alternative:
sed -n '4,${s/^\(\(\S\+\)\s*\)\{3\}.*//p;n}' file
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论