Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy/delete parameters from command stream!

Status
Not open for further replies.

janise

Technical User
May 25, 2003
161
US
Hello AWK Gurus,
The command stream is stored in a file and looks like this:
-CT LATM -CS -CD DEMO -CA RACK -CAP TOGGLE -RP GLS7002 -I 372 -R test -CO USER -OT 6 -OP /src/test/log/TI09887 2 -OF 2 -DE "This is a test message"

I need to identify the paramater after -I, -CO and -DE flags and put them in vaiables, e.g. in this case:
$VAR1 = 372
$VAR2 = USER
$VAR3 = This is a test message

Also, I need to edit the command by deleting this bit and putting it to another file:
-DE &quot;This is a test message&quot; <- Remove this part

Thanks for your help!
Janise
 
You can do it statically, pretty simply, if you can always anticipate the length of the variable to be cut from the
command string.

Say:
awk '
/-DE/ { array[&quot;DE&quot;] = match($0,/-DE/) }
/-I/ { array[&quot;I&quot;] = match($0,/-I/) }
/-CO/ { array[&quot;CO&quot;] = match($0,/-CO/) }
{
for (all in array) {
if (all == &quot;CO&quot;) {
printf &quot;var1=%s\n&quot;,substr($0,array[all] + 4,4)
}
if (all == &quot;I&quot;) {
printf &quot;var2=%s\n&quot;,substr($0,array[all] + 3,3)
}
if (all == &quot;DE&quot;) {
printf &quot;var3=%s\n&quot;,substr($0,array[all] + 4,25)
printf substr($0,array[all] + 4,25) > &quot;out.txt&quot;
}
}
}'

Output looks like:
echo $mess | mess.awk
var1=USER
var3=&quot;This is a test message&quot;
var2=372

To introduce the variables:
1.a=`echo $mess | mess.awk`
2.echo $a
var1=USER var3=&quot;This is a test message&quot; var2=372
3.eval $a
4. echo $var1
USER

Otherwise if you have to worry about a different variable length to be cut from the string each time it is more problematic.
I'm sure someone else will have a better solution.

 
Hi:

Marsd's awk solution is good, probably better than this:

For years, I used the shift; var; shift; technique to parse command line arguments. I've never really come up with a good solution for handling:

-DE &quot;this is atest file&quot;

The following Korn Shell sub kinda/sorta works:

Regards,


Ed

#!/bin/ksh

# return true if a quote is in the string
# else false
function is_quote {
x=$(echo $1|awk ' { if($0 ~ /[\&quot;]/ )
print 1
else
print 0 } ')
echo $x
}

# get the command line arguments from data file setting
# command line arguments
set - `cat data.file`

#
while [ &quot;$#&quot; -gt 0 ]
do # set the command line arguments
case $1 in
# -I
-I) shift
VAR1=$1
shift
;;
# -CO
-CO) shift
VAR2=$1
shift
;;
-DE) shift
VAR3=$1
ctr=$(is_quote $VAR3)
if [[ $ctr -eq 1 ]]
then # loop until you find terminating quote
ctr=0
until [ &quot;$ctr&quot; -eq 1 ]
do
shift
ctr=$(is_quote $1)
if [[ $ctr -eq 0 ]]
then
VAR3=$VAR3&quot; &quot;$1
fi
done
VAR3=$VAR3&quot;\&quot;&quot;
fi
shift
;;
*) shift # ignore the unneeded options
;;
esac
done
if ! [ -z &quot;$VAR1&quot; ]
then # echo if set
echo $VAR1
fi
if ! [ -z &quot;$VAR2&quot; ]
then # echo if set
echo $VAR2
fi

if ! [ -z &quot;$VAR3&quot; ]
then # echo if set
echo $VAR3
fi

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top