Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a 'where' clause in Linux shell?

Tags:

linux

bash

shell

I have a CSV file and I would like to filter all the lines where the 19th column has two or more characters. I know the individual pieces but can't figure out how to glue them together. First I have to cat the file. The following prints the 19th column

awk -F "," '{print $19}' file.txt 

awk also has length and ifs

And I know it all has to be glued together using pipes. I'm just getting stuck at the exact syntax since I have not done much bash programming before.

like image 561
Hoa Avatar asked Jan 17 '23 02:01

Hoa


2 Answers

An AWK program is a series of pattern action pairs, written as:

condition { action }

The condition part is your filter.

For example, to get all lines(19th column has at least 2 chars):

$ awk -F, 'length($19)>1' file.txt

When {action} part is missing, the default action is to print the record.

like image 133
kev Avatar answered Jan 29 '23 20:01

kev


This should work:

awk -F ',' '{if (length($19) < 2) { print $0 }}' file.txt

It prints the whole line if the length of the 19th field is smaller than 2.

like image 34
Friek Avatar answered Jan 29 '23 21:01

Friek