LINUX从列表中删除行

LINUX从列表中删除行

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

问题描述

      $ 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从列表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:40