Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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 –  Michael Mrozek Feb 22 '12 at 18:43

3 Answers 3

up vote 6 down vote accepted
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
share|improve this answer

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
share|improve this answer

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'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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