使用sort,grep LINUX从列表中删除行 [英] removing line from list using sort, grep LINUX

查看:617
本文介绍了使用sort,grep LINUX从列表中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #人口,年份,县
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

这个清单要长得多,但我没有包括其余的内容。我使用grep过滤列表以获得仅1989年,然后按人口从大多数填充到最少填充的人群进行排序,但我遇到了问题。



<$ p $ 1989年,盐湖
260368,1989年,犹他州
219893,1980年,犹他州
185236年,1989年,戴维斯
157847,1989,韦伯

正如您所看到的,第三行是1980年,并且正在显示,因为人口是 219893 ,其中包含 1989 的编号。

我使用这个命令:
grep1989utah.txt | sort -nk1,1 -r



这些东西是否可以用来摆脱那条线?



您的帮助表示感谢。 您可能需要在您的<$ c中使用 -w $ C>的grep 。这样它就会匹配整个单词:

  $ grep -w 1989文件
719048,1989,Salt Lake
260368,1989,犹他州
185236,1989,Davis
157847,1989,Weber

注意区别:

  $ grep 1989文件
719048,1989,Salt Lake
260368,1989,Utah
219893,1980,Utah< ---- without -w,这匹配
185236,1989,Davis
157847,1989,Weber

如果需要, awk 也可以通过指示您正在检查的字段:

  $ awk'$ 2〜1989'文件
719048,1989,Salt Lake
260368,1989,犹他州
185236,1989,Davis
157847,1989,Weber


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   <---- without -w, this matches
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

这篇关于使用sort,grep LINUX从列表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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