Re: string manipulation inquiry (84 Views)
Reply
Regular Advisor
Pando
Posts: 201
Registered: ‎04-15-2004
Message 1 of 9 (84 Views)
Accepted Solution

string manipulation inquiry

Dear Gurus,

I have a file with the following contents,

...
...
PRODUCT,XXXYYYZZZ_75_HD20
...
...

i have used awk to get the content of the product field (which is XXXYYYZZZ_75_HD20).

export Var=$(awk -F "," '/PRODUCT/ {print $2}' $FILE)

After getting the product content, i have to manipulate the result by truncating the "_HD20"
which is a contant in some devices (others dont have).

I need to write a script in which it has to test if the content of the product has the "_HD20" strings and eventually put the product in a variable and compare it to other file for matching.

Maximum points for all correct answers!
Please use plain text.
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 2 of 9 (84 Views)

Re: string manipulation inquiry

var='PRODUCT,XXXYYYZZZ_75_HD20'
var1=${var1##*,}
check=`awk -F "_" '{print $3}'
if [[ ${check} = "HD20" ]]
then
your_code here
else
some more code
fi

There is no substitute to HARDWORK
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 3 of 9 (84 Views)

Re: string manipulation inquiry

how about just doing the substring twice

var='PRODUCT,XXXYYYZZZ_75_HD20'
var1=${var##*,}
check=${var1##*_}
if [[ ${check} = "HD20" ]]
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 4 of 9 (84 Views)

Re: string manipulation inquiry

and if you just want to use awk
cat yourFile |
awk -f "," '
/PRODUCT/ {
split($2,a,"_");
print $2, a[1] a[2], a[3];
}' |
while read a b c
print $a $b $c
done
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 5 of 9 (84 Views)

Re: string manipulation inquiry

to truncate

var='PRODUCT,XXXYYYZZZ_75_HD20'
var1=${var##*,}
check=${var1##*_} #remove everything from the begining of the line to the last underscore "_". will be HD20 if it is there
rest=${var1%_*) #remove everything from the end of the line up and including the "_". will be XXXYYYZZZ_75
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 6 of 9 (84 Views)

Re: string manipulation inquiry

You can try like,

export Var=$(awk -F, '/PRODUCT/ { if ( $2 ~ /HD20/ ) { print $2 }}' $FILE)

where,

a) awk splits with ","
b) check's PRODUCT keyword
c) Prints XXXYYYZZZ_75_HD20 when second field contains HD20 keyword

else Var will empty.

You can check like,

if [ "$var" = "" ]
then
# var is empty
else
# var is with XXXYYYZZZ_75_HD20
fi

hth.
Easy to suggest when don't know about the problem!
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 7 of 9 (84 Views)

Re: string manipulation inquiry

You can very well use perl also as,

export product=$(perl -ne 'split /,/; print $_[1] if /PRODUCT,.*HD20/' $FILE)

with check code.

hth.
Easy to suggest when don't know about the problem!
Please use plain text.
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 8 of 9 (84 Views)

Re: string manipulation inquiry

Simple awk format as,

export Var=$(awk -F, '/PRODUCT,.*HD20/ { print $2; }' $FILE)

Or else you can directly check it as,

if [ $(awk -F, '/PRODUCT,.*HD20/ { print $2; }') = "" ]
do
# Nothing there
else
# There is HD20
fi

hth.
Easy to suggest when don't know about the problem!
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 9 (84 Views)

Re: string manipulation inquiry

Hi:

Curt Larson's first solution is certainly the fastest and "cheapest". It leverages shell builtins and therefore runs the faster than deploying 'awk' or 'perl' for the simple task.

Regards!

...JRF...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation