使用 grep 函数保留特定行 [英] Keeping specific rows with grep function

查看:60
本文介绍了使用 grep 函数保留特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据集并且变量包含不同的格式

I have a large data sets and the variable includes different format

Subject   Result
1           3
2           4
3          <4
4          <3
5          I need to go to school<>
6          I need to <> be there
7          2.3 need to be< there
8          <.3
9          .<9
10         ..<9
11         >3 need to go to school
12         <16.1
13         <5.0

我只想保留包含<数字"或>数字"的行,而不是具有文本格式的行(例如,我想排除 >3 需要上学,我需要去上学<>).问题是有些记录类似于 .<3, ..<9, >9., >:9.那么如何从数据集中删除."、.."、:",然后用<一个数字"表示法保留行.如何使用grep"功能?同样,我只想保留以下行

I just want to keep the rows which include the "< number" or "> number" and not the rows with the text format (forexample, I want to exclude >3 need to school, I need to go to school <>). The problem is that some records are something like .<3, ..<9, >9., >:9. So how can I remove ".","..",":" from the data set and then keep the rows with "< a number" notation. How can I use "grep" function? Again, I just want to keep the following rows

    Subject   Result
>     3          <4
>     4          <3
>     8          <.3
>     9          .<9
>     10         ..<9
>     12         <16.1
>     13         <5.0

推荐答案

您可以简单地应用两个 grep,一个用于查找 "<>" 键,然后一个用于消除带有字符的字段:

You can simply apply two greps, one to find the "<>" keys, and then one to eliminate fields with characters:

grep "[><]" |grep -v "[A-Za-z]"

grep "[><]" | grep -v "[A-Za-z]"

如果你想学究,你也可以应用另一个grep来找到那些有数字的

If you want to be pedantic, you can also apply another grep to find those with numbers

grep "[><]" |grep -v "[A-Za-z]" |grep "[0-9]"

grep "[><]" | grep -v "[A-Za-z]" | grep "[0-9]"

顺便说一下,grep -v"表示匹配并且不返回.

"grep -v" means match and don't return, by the way.

这篇关于使用 grep 函数保留特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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