Selecting a part of the text (regex pattern, awk, sed)

Hello,

let's start by giving you guys a few examples of the text:

"READ /TEXT123/ABC123"
"READ /TEXT123/ABC123/"
"READ TEXT123/ABC123"
"READ TEXT123/ABC123/"
"READ TEXT123/TEXT456/ABC123"
"READ /TEXT123/TEXT456/ABC123"
"READ /TEXT123/TEXT456/ABC123/"

TEXT and ABC can be [a-zA-Z0-9._-()] and I need to assign both into a separated variable..

    TEXT=`echo $1 | awk -F" " '{print $2}' | sed 's/^\/\?\(.*\)\/\(.*\)\/\?$/\1/'`
    ABC=`echo $1 | awk -F" " '{print $2}' | sed 's/^\/\?\(.*\)\/\(.*\)\/\?$/\2/'`

That's how far I got and it works almost for them all, beside the ones with a / in the end..

I'd really appreciate any help, especially from all the one-liner experts.. oh and thanks in advance :wink:

-F" " is useless, because it is default FS

And you need tell use your expect O/P

for example, for this string: /TEXT123/TEXT456/ABC123/

what will be TEXT and ABC?

Seems basename and dirname will help you.

---------- Post updated at 11:00 AM ---------- Previous update was at 10:55 AM ----------

cat infile |while read LINE; 
do  
  TEXT=$( echo $LINE|awk '{gsub(/"/,"");print $2}' |xargs dirname)
  ABC=$(echo $LINE|awk '{gsub(/"/,"");print $2}' |xargs basename)
done
"READ /TEXT123/ABC123"
TEXT= TEXT123
ABC= ABC123

"READ TEXT123/ABC123/"
TEXT= TEXT123
ABC= ABC123

"READ /TEXT123/TEXT456/ABC123/"
TEXT= TEXT123/TEXT456
ABC= ABC123

Oh you prolly misunderstood the " " part in my text examples, basically my script gets executed by another one and that script is passing two arguments where the first one contains my examples... so there arn't any real "" in the text... $1 = READ /TEXT123/ABC123

with orig code, no hurt for the result.

If there is no ", remove the gsub function from awk.

cat infile |while read LINE; 
do  
  TEXT=$(echo $LINE|awk '{print $2}' |xargs dirname)
  ABC=$(echo $LINE|awk '{print $2}' |xargs basename)
done

Okey it's almost perfect, in this example:

"READ /TEXT123/TEXT456/ABC123/" 
TEXT= TEXT123/TEXT456 ABC= ABC123

it gives back TEXT = /TEXT123/TEXT456 while I need it without the first / at all times...

TEXT=$(echo $LINE|awk '{print $2}' |xargs dirname||sed 's#^/##' )