巴什 - 以第n列在一个文本文件 [英] Bash - Take nth column in a text file

查看:82
本文介绍了巴什 - 以第n列在一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件:

1 Q0 1657 1 19.6117 Exp
1 Q0 1410 2 18.8302 Exp
2 Q0 3078 1 18.6695 Exp
2 Q0 2434 2 14.0508 Exp
2 Q0 3129 3 13.5495 Exp

我想利用这样的每一行的第2和第4个字:

i want to take 2nd and 4th word of every line like this:

1657 19.6117
1410 18.8302
3078 18.6695
2434 14.0508
3129 13.5495

İ'm使用这种code

İ'm using this code

 nol=$(cat "/path/of/my/text" | wc -l)
 x=1
 while  [ $x -le "$nol" ]
 do
 line=($(sed -n "$x"p /path/of/my/text)
 echo ""${line[1]}" "${line[3]}""  >> out.txt
 x=$(( $x + 1 ))
 done

它的工作原理,但是很复杂,并且需要很长的时间来处理长文本文件。
有一个简单的方法来做到这一点?
谢谢你。

It works but very complicated and takes long time to process long text files. Is there a simple way to do this? Thanks.

推荐答案

IIRC:

cat filename.txt | awk '{ print $2 $4 }'

,或如在评论中提到:

or, as mentioned in the comments :

awk '{ print $2 $4 }' filename.txt

这篇关于巴什 - 以第n列在一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆