tags:

views:

175

answers:

3

What I need to do is parse a string of the following form

-option optionArgument, --alternativeNotation Some text, nothing of interest...

I set the FS to

BEGIN {
    FS = ",?\ +" 
}

but it didn't work... it should break on every random number of blanks (at least one), preceeded by a comma (optional). Any ideas?

Thx in advance,

Oliver

+1  A: 

FS = "[,]*[ ]+"

This makes the comma optional but not the space. This creates a separate field out of each -option and optionArg which is what I believe you wanted.

awk 'BEGIN {FS = "[,]*[ ]+";} { print $1; print $2; print $3; print $4; print $5;}' << EOF
> -option1 hello, --option2 world, -option3
> EOF
-option1
hello
--option2
world
-option3
Rob Curtis
Incorrect. With or without character classes, Trollhorn's patterns are semantically equivalent.
Greg Bacon
Good point. I'd never not used character classes.
Rob Curtis
+1  A: 

Your FS does what you describe in your question, but the backslash before the space might be redundant depending on shell quoting:

$ echo '-option optionArgument, --alternativeNotation Some text, nothing of interest...' |  \
  nawk 'BEGIN {
          FS=",? +";
          OFS="][";
        }
        {
          print "["$1,$2,$3,$4"]";
          print "["$5,$6,$7,$8"]";
        }'
[-option][optionArgument][--alternativeNotation][Some]
[text][nothing][of][interest...]

What do you want the fields to be?

Greg Bacon
A: 

@OP, next time try to describe what your final output is.

echo "-option1 hello,          --option2 world, -option3" | awk 'BEGIN{FS=",[ ]+"}
{
    for(i=1;i<=NF;i++){
        print $i
    }
}
'

output

$ ./shell.sh
-option1 hello
--option2 world
-option3

Also, there is actually no need to check for multiple blanks. Just use comma as the delimiter and trim the remaining spaces later.

echo "-option1 hello,          --option2 world, -option3" | awk 'BEGIN{FS=","}
{
    for(i=1;i<=NF;i++){
        gsub(/^ +| +$/,"",$i)
        print $i
    }
}
'

output

$ ./shell.sh
-option1 hello
--option2 world
-option3
ghostdog74