Marta Koprivnik
Marta Koprivnik

Reputation: 405

Bash: How to keep line if number in second column is greater than 3?

What is the best way to keep line if number in second column (separated by space) is greater than 3?

And I MUST NOT use awk or sed! -.-

Input:

2 2 asd 132ds
1 4 sdf 234sd
1 3 gfd 654gh
1 1 rtz 543jh
1 10 uio 654iu

Output:

1 4 sdf 234sd
1 10 uio 654iu

Upvotes: 2

Views: 655

Answers (3)

jaypal singh
jaypal singh

Reputation: 77135

Using grep to filter out unwanted requirements

$ cat file
2 2 asd 132ds
1 4 sdf 234sd
1 3 gfd 654gh
1 1 rtz 543jh
1 10 uio 654iu

$ egrep -v '^[^.]+ [1-3] ' file
1 4 sdf 234sd
1 10 uio 654iu

Upvotes: 2

Anuj
Anuj

Reputation: 11

Using while loop

while read line; do if [ $(echo $line|cut -d " " -f2) -gt 3 ]; then echo $line; fi; done < testfile.txt

Upvotes: 1

heemayl
heemayl

Reputation: 42087

Use a while loop:

while read -r i j k; do [ "$j" -gt 3 ] && echo "$i $j $k"; done <file.txt

Upvotes: 3

Related Questions