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.

I'm trying to use regex as a field seperator in awk. From my reading this seems possible but I can't get the syntax right.

rpm -qa | awk '{ 'FS == [0-9]' ; print $1 }'
awk: cmd. line:1: { FS
awk: cmd. line:1:     ^ unexpected newline or end of string

Thoughts? The goal if not obviouse is to get a list of software without version number.

share|improve this question

2 Answers 2

up vote 11 down vote accepted

You have mucked up your quotes. Here's a better way:

awk -F'[0-9]' '{ print $1 }'
share|improve this answer

+1 for KAK's answer. Alternately, the FS variable can be set in the BEGIN block:

awk 'BEGIN {FS="[0-9]"} {print $1}'

Changing FS in a action block won't take effect until the next line is read

$ printf "%s\n" "abc123 def456" "ghi789 jkl0" | awk '{FS="[0-9]"; print $1}'
abc123
ghi

The other errors in the question:

  • can't use single quotes inside a single-quoted string
  • == is a comparison operator, = is for variable assignment
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.