在txt文件的每一行中打印第一个单词unix bash [英] printing first word in every line of a txt file unix bash

查看:56
本文介绍了在txt文件的每一行中打印第一个单词unix bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试在txt文件的每一行中打印第一个单词.单词之间用一个空格隔开.

So I'm trying to print the first word in each line of a txt file. The words are separated by one blank.

cut -c 1 txt file 

那是我到目前为止的代码,但是它只打印每行的第一个字符. 谢谢

Thats the code I have so far but it only prints the first character of each line. Thanks

推荐答案

要打印整个单词,您需要-f 1,而不是-c 1.而且由于默认的字段定界符是TAB而不是SPACE,因此您需要使用-d选项.

To print a whole word, you want -f 1, not -c 1. And since the default field delimiter is TAB rather than SPACE, you need to use the -d option.

cut -d' ' -f1 filename

要打印使用cut的AFAIK不可能完成的最后两个单词,因为它只能从行的开头开始计数.使用awk代替:

To print the last two words not possible with cut, AFAIK, because it can only count from the beginning of the line. Use awk instead:

awk '{print $(NF-1), $NF;}' filename

这篇关于在txt文件的每一行中打印第一个单词unix bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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