SED 命令获取 x 行和 y 行之间的第 n 个制表符分隔值
我已经能够从一个大型制表符分隔的文本文件中提取某些行并将它们写入另一个文件:
sed -n 100,200p file.tsv >> output.txt
但是,我实际上试图从每行获取第 8 个制表符分隔值并将它们写入到一个以逗号分隔的文件,但尽管阅读了数十篇在线文章,但我找不到用于模式匹配的正确语法。
每次我基本上都在尝试匹配
/([^\t]*\t){7}([0-9]*).*/
$2 >
没有运气。
文本文件 file.tsv 中的行类似于:
01 name1 title1 summary1 desc1 image1 url1 120019 time1
02 name2 title2 summary2 desc2 image2 url2 576689 time2
请问任何人都可以帮助我完成此查询吗?
I have been able to extract certain lines from a large tab-separated text file and write them to another file:
sed -n 100,200p file.tsv >> output.txt
However, I am actually trying to grab the 8th tab-separated value from each line and write them to a file comma separated, but I cannot find the right syntax to use for the pattern matching, despite reading dozens of online articles.
For each time I have basically been trying to match
$2
in /([^\t]*\t){7}([0-9]*).*/
with no luck.
The lines within the text file file.tsv resemble:
01 name1 title1 summary1 desc1 image1 url1 120019 time1
02 name2 title2 summary2 desc2 image2 url2 576689 time2
Please can anyone help me with this query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想我宁愿这样使用 awk:
我想未来的工作会更容易。
I think I would rather use awk that way:
The forward work will be easier I guess.
Perl 一行代码:
A Perl one-liner:
这里它使用 GNU sed 和扩展表达式:
这里它仅使用 POSIX:
我确实同意 Alf 的观点,即
awk
更适合于此。这是带有行限制的
awk
解决方案:Here it is using GNU sed and extended expressions:
Here it is using POSIX only:
I do agree with Alf that
awk
would be a better fit for this.Here is the
awk
solution with line limits:如果有空字段,这将起作用。
This will work if there are empty fields.