tags:

views:

103

answers:

2

I'm writing a shell script and I need to strip FIND ME out of something like this:

* *[**FIND ME**](find me)*

and assign it to an array. I had the code working flawlessly .. until I moved the script in Solaris to a non-global zone. Here is the code I used before:

objectArray[$i]=`echo $line | nawk -F '*[**|**]' '{print $2}'`

Now Prints:

awk: syntax error near line 1  
awk: bailing out near line 1

It was suggested that I try the same command with nawk, but I receive this error now instead:

nawk: illegal primary in regular expression `* *[**|**]` at  `*[**|**]`
 input record number 1
 source line number 1

Also, /usr/xpg4/bin/awk does not exist.

+1  A: 

I think you need to be clearer on what you want to get. For me your awk line doesn't 'strip FIND ME out'

echo "* *[**FIND ME**](find me)*" | nawk -F '* *[**|**]' '{print $2}'
[

So it would help if you gave some examples of the input/output you are expecting. Maybe there's a way to do what you want with sed?

EDIT:

From comments you actually want to select "FIND ME" from line, not strip it out.

I guess the dialect of regular expressions accepted by this nawk is different than gawk. So maybe a tool that's better suited to the job is in order.

echo "* *[**FIND ME**](find me)*" | sed -e"s/.*\* \*\[\*\*\(.[^*]*\)\*\*\].*/\1/"
FIND ME
Douglas Leeder
It worked for me with awk, I have not successfully tested the nawk output. Here's what I want:I know that $line=* *[**FIND_ME**](find_me)*and I want to assign objectArray[$i]=FIND_MESed might work, I will give this a try.
pws5068
So you actually want to select "FIND_ME" from such lines?
Douglas Leeder
Sorry, Try This: echo "* *[**FIND_ME**](find_me)*" | awk -F '*[**|**]' '{print $2}'
pws5068
Yes, I want to select FIND_ME.
pws5068
+1  A: 

quote your $line variable like this: "$line". If still doesn't work, you can do it another way with nawk, since you only want to find one instance of FIND ME,

$ echo "$line" | nawk '{gsub(/.*\*\[\*\*|\*\*\].*/,"");print}'
FIND ME

or if you are using bash/ksh on Solaris,

$ line="${line#*\[\*\*}"
$ echo "${line%%\*\*\]*}"
FIND ME
ghostdog74
The shell-only solution should also work with ksh, which should be on Solaris
glenn jackman
Ought to use double quotes: `line="${line#*[\*\*}"; echo "${line%%\*\*]*}"`
glenn jackman
Hmm, need more backslashes here too: `line="${line#*\[\\*\\*}"; echo "${line%%\\*\\*\]*}"`
glenn jackman
what shell did you use? I have no problem with my bash shell. However, i will just edit it.
ghostdog74