Reputation: 9
I have a data file with a list of numbers like that:
5 5
6 6
4 4
3 3
4 4
3 3
2 2
I wish to pick out the first row that has the two columns with numbers <=3 and delete all the rows after that. Hence my result file will just be:
5 5
6 6
4 4
3 3
Does anyone know how to use awk/sed for this?
Upvotes: 0
Views: 289
Reputation: 226516
For Python, print every line and just break out of the loop when your criteria is met:
>>> while open('somefile.txt') as f:
for line in f:
print(line, end='')
p, q = map(int, line.split())
if p <= 3 or q <= 3:
break
5 5
6 6
4 4
3 3
Here is an AWK solution:
$ awk 'BEGIN {flag=0} flag==0 {print ; flag=$1<=3 && $1<=3}' somefile.txt
5 5
6 6
4 4
3 3
Upvotes: 0
Reputation: 37424
Here's an awk solution:
$ awk '1; $1<=3&&$2<=3{exit}' file
5 5
6 6
4 4
3 3
Explained:
awk '
1; # output record
$1 <= 3 && $2 <= 3 { exit } # if it was as requested, exit
' file
Upvotes: 1
Reputation: 92874
Consider the following sed approach:
sed -n '1,/[0-3] [0-3]/p' file
The output:
5 5
6 6
4 4
3 3
1,/[0-3] [0-3]
- address range starting from the first line and captures lines till encountering 2 digits which are less or equal to 3
(inclusively).
An address range is specified with two addresses separated by a comma (,). Addresses can be numeric, regular expressions, or a mix of both.
Upvotes: 1