grep 去除特定行,使用sort,grep LINUX从列表中删除行

I have a list that looks like:

#Population, Year, County

3900, 1969, Beaver

3798, 1970, Beaver

3830, 1971, Beaver

3864, 1972, Beaver

3993, 1973, Beaver

3976, 1974, Beaver

4064, 1975, Beaver

4074, 1976, Beaver

4064, 1977, Beaver

4194, 1978, Beaver

4240, 1979, Beaver

The list is much longer but I did not include the rest. I used grep to filter the list to get only the years 1989, and then sorted it by population going from most populated to least populated, but I encountered an issue.

719048, 1989, Salt Lake

260368, 1989, Utah

219893, 1980, Utah

185236, 1989, Davis

157847, 1989, Weber

As you can see, the third line is the year 1980, and it is being displayed because the population is 219893 and that has the number 1989 in it. How do I filter this line out?

I used this command:

grep "1989" utah.txt | sort -nk1,1 -r

Is these something else I could use to get rid of that line?

Your help is appreciated.

解决方案

You may need to use -w in your grep. This way it will just match whole words:

$ grep -w 1989 file

719048, 1989, Salt Lake

260368, 1989, Utah

185236, 1989, Davis

157847, 1989, Weber

Notice the difference:

$ grep 1989 file

719048, 1989, Salt Lake

260368, 1989, Utah

219893, 1980, Utah

185236, 1989, Davis

157847, 1989, Weber

If you want, awk can also do the job by indicating the field you are checking:

$ awk '$2~1989' file

719048, 1989, Salt Lake

260368, 1989, Utah

185236, 1989, Davis

157847, 1989, Weber

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值