0

I have file with following format :

Q
L
A D
1 10
2 21
3 22
4 5
5 9

I need to skip lines which starts with 'Q' or 'L' and delete lines where second column has value greater than 10 and save the entire thing in new file. Example output files :

Output file 1
    Q
    L
    A D
    1 10
    4 5
    5 9 

Output file 2
1
4
5

Code :

while read -r line; if [[ $line == "A" ]] ||[[ $line == "Q" ]]||[[ $line == "L" ]] ; then 
awk '$2 < "11" { print $0 }' test.txt
awk '$2 < "11" { print $1 }' test1.txt
done < input.file
  • It's unclear what you want, please describe what's in output file 1 and output file 2 separately. – dedowsdi May 23 '19 at 23:43
0

You could use a simple regular expression to filter for lines that start with letters A or L or Q plus a condition. awk is a good fit for something like this:

awk '($0 ~ /^[AQL]/ || $2 < 11) { print $0 }' < input.file > output.file1
awk '($0 ~ /^[^AQL]/ && $2 < 11) { print $1 }' < input.file > output.file2
  • @Thor, thanks, I updated my answer. – Martin May 28 '19 at 13:50
0

Tested with below command and it worked fine

command

awk '$1 !~ "Q|L" && $2 <11{print $1}' filename

output

awk '$1 !~ "Q|L" && $2 <11{print $1}' filename
1
4
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.