Issue
as tittle, how sed perform "greater than" to delete specific line?
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. For example, I wanna keep those lines which contains string1-5 and delete string6-string99, I'd tried:
#!/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?
Solution
This 'awk` should do:
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
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
Answered By - Jotne Answer Checked By - Timothy Miller (WPSolving Admin)