In awk. I am working on Solaris 10, so it's probably an old(er) version of awk. I came up with this rudimentary one-liner that works, at least for my particular input.

awk -F\; '$3 ~ /[ ]*...............................*/' file.csv 

There may or may not be spaces around the separators hence the [ ]* part of the regex.

Wanting to avoid printing 30 times the dot . character, I tried the following:

awk -F\; '$3 ~ /[ ]*.\{30\}.*/{print $3}' file.csv

This did not return any result. If it helps in any way, I'm using ksh88.

What would be the best way to avoid entering those 30 consecutive dots?

  • It doesn't answer your question, but on Linux the awk man page says you need to pass --posix or --re-interval to enable interval expressions like {30}. I don't have a Solaris 10 box to try it on, but the awk on Solaris 8 doesn't support them Commented Feb 22, 2012 at 18:43

3 Answers 3

awk -F\; 'length($3) == 30' file.csv

If you may have white space (as it appears) you can get rid of it by making it part of the field separator:

awk -F' *; *' 'length($3)==30' file.csv

GNU awk didn't always match interval expressions, which is what the {n} syntax is requesting. POSIX compatible awks might accept -W re-interval to enable the syntax, so try that. But you might be running System V derived awk that doesn't accept interval syntax at all.

I think your best bet is to set FS to a regular expression, which will let you treat the spaces around the semicolons as part of the field separator. This is a much older awk feature and is likely to be supported in the awk that you have. With the spaces out of the way you can dispense with the regexp full of dots and use the length() function to check the length of the field.

awk -F"[ ]*;[ ]*" 'length($3) == 30 { print $3 }' file.csv

If I understand correctly, you want to print out the lines where the third field is at least 30 characters long, excluding surrounding whitespace.

awk -F ';' 'match($3, "[^ ].*[^ ]") && RLENGTH >= 30'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .