[英]How to perform "greater than" with sed or awk to delete specific lines?
as tittle, how sed perform "greater than" to delete specific line?作为标题,sed 如何执行“大于”以删除特定行?
I got a file like this:我有一个这样的文件:
bash-4.2$ cat testfile.txt
string1 1 1
AAA 2 2
string2 2 2
BBB 3 3
string3 3 3
string4 4 4
string5 5 5
string6 6 6
CCC 6 6
string7 7 7
string8 8 8
string9 9 9
string10 10 10
DDD 11 11
string11 11 11
string12 12 12
string13 13 13
I wanna delete some lines which contains "string[[:digit:]]" but string1 to string"$num" is needed, num is defined by a variable.我想删除一些包含“string[[:digit:]]”的行,但是需要 string1 到 string“$num”,num 由一个变量定义。 For example, I wanna keep those lines which contains string1-5 and delete string6-string99, I'd tried:
例如,我想保留那些包含 string1-5 并删除 string6-string99 的行,我试过:
#!/bin/bash
read -p "Please Assign the Number of String Line that You Wanna Keep: " num
cat testfile.txt | sed -e "/string[`expr $num + 1`-9]/d" > new_testfile.txt
but it's only working in range 0-8, is there any way to perform it with sed or awk?但它只在 0-8 范围内工作,有没有办法用 sed 或 awk 来执行它?
This 'awk` should do: 该“ awk”应该执行以下操作:
awk '/^string/ {n=substr($1,7)+0;if (n>5 && n<100) next}1' file
string1 1 1
AAA 2 2
string2 2 2
BBB 3 3
string3 3 3
string4 4 4
string5 5 5
CCC 6 6
DDD 11 11
It just skips any line with string"x"
where x
is larger then 5
and less then 100
它只跳过
string"x"
任何行,其中x
大于5
小于100
If high/low comes from variables, this should do: 如果高/低来自变量,则应这样做:
high=99
low=6
awk '/^string/ {n=substr($1,7)+0;if (n>=l && n<=h) next}1' h="$high" l="$low" file
string1 1 1
AAA 2 2
string2 2 2
BBB 3 3
string3 3 3
string4 4 4
string5 5 5
CCC 6 6
DDD 11 11
Here is one way with awk
: 这是
awk
一种方法:
$ read -p "Please Assign the Number of String Line that You Wanna Keep: " num
Please Assign the Number of String Line that You Wanna Keep: 5
$ awk -v max="$num" '/string/{line=$0;sub(/string/,"",$1);if($1+0<=max){print line};next}1' file
string1 1 1
AAA 2 2
string2 2 2
BBB 3 3
string3 3 3
string4 4 4
string5 5 5
CCC 6 6
DDD 11 11
This is an old question, but if you are coming from a duplicate, perhaps the important thing to understand is that sed
does not have any facilities for arithmetic, which is why all the old answers here use Awk.这是一个老问题,但如果你来自重复问题,也许重要的是要理解
sed
没有任何算术工具,这就是为什么这里所有的旧答案都使用 Awk。
If you can articulate a regex which reimplements your mathematical constraint as a textual constraint, these things are possible;如果您可以阐明一个正则表达式,将您的数学约束重新实现为文本约束,那么这些事情都是可能的;
sed 's/string\([1-9][0-9]\+\|[6-9]\)//' testfile.txt
To briefly spell this out, this finds and replaces string
if it is followed by two or more digits, or a single digit which is 6-9
, which implements the requirement by matching the digits as string sequences.简而言之,如果字符串后跟两个或更多数字,或者一个数字
6-9
,它会查找并替换string
,它通过将数字匹配为字符串序列来实现要求。
GNU sed
also has a limited facility for executing external commands on the matched text with the non-standard flag /e
, but my advice would be to switch to Awk at that point anyway, which allows you to reason about mathematical properties of numbers with a more readable and beginner-friendly syntax as well as vastly better efficiency by way of avoiding to spawn an external process for each expression you want to evaluate. GNU
sed
在使用非标准标志/e
的匹配文本上执行外部命令的功能也很有限,但我的建议是无论如何在那时切换到 Awk,这样你就可以用通过避免为每个要评估的表达式生成外部进程,语法更具可读性和初学者友好性,并且效率大大提高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.