Operating System - Linux
1751936 Members
5007 Online
108783 Solutions
New Discussion юеВ

Re: string manipulation inquiry

 
SOLVED
Go to solution
Pando
Regular Advisor

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!
8 REPLIES 8
RAC_1
Honored Contributor
Solution

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
curt larson_1
Honored Contributor

Re: string manipulation inquiry

how about just doing the substring twice

var='PRODUCT,XXXYYYZZZ_75_HD20'
var1=${var##*,}
check=${var1##*_}
if [[ ${check} = "HD20" ]]
curt larson_1
Honored Contributor

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
curt larson_1
Honored Contributor

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
Muthukumar_5
Honored Contributor

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!
Muthukumar_5
Honored Contributor

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!
Muthukumar_5
Honored Contributor

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!
James R. Ferguson
Acclaimed Contributor

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...