1847030 Members
4215 Online
110259 Solutions
New Discussion

usinf sed

 
SOLVED
Go to solution
Henry Chua
Super Advisor

usinf sed

Hi Guys,

I'll need to extract data from the following field "WJV82LV83A1.ICZ34K.ws". The data I need is captured between W and the . , i.e. JV82LV83A in this case. as the data may differ in lenght i tried using a combo of awk and sed to do it.. but the script got quite bulky though it did the job., Is there a easier way. I'll need to reduce the speed and alot of data to capture. any advise?

thank u

11 REPLIES 11
Olivier LEGRAND
Frequent Advisor

Re: usinf sed

Hi

echo "WJV82LV83A1.ICZ34K.ws"|awk -F"." '{print $1}'|cut -c2-

Best regards
Steve Steel
Honored Contributor

Re: usinf sed

Hi


Use the SHELL variable IFS and cut

Or cut with the -d option if the
data field format is stable

echo "WJV82LV83A1.ICZ34K.ws". |cut -f1 -d"."|cut -f2 -d"\""

Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Muthukumar_5
Honored Contributor

Re: usinf sed

Try this as,
echo "WJV82LV83A1.ICZ34K.ws"| awk -F"." '{ print substr($1,2); }'

HTH.
Easy to suggest when don't know about the problem!
Gordon  Morrison_1
Regular Advisor

Re: usinf sed

Hi Henry,
Assuming you always want to drop the first character and keep everything after it up until the first dot (.) then:

str1="WJV82LV83A1.ICZ34K.ws"
tmp=${str1#?}
echo $tmp
JV82LV83A1.ICZ34K.ws
str2=${tmp%%.*}
echo $str2
JV82LV83A1
What does this button do?
Muthukumar_5
Honored Contributor

Re: usinf sed

IF you are date contains W*****.I** then, it is easy to do with sed as,

echo "WJV82LV83A1.ICZ34K.ws"| sed 's/^W\(.*\).I.*/\1/'

HTH.
Easy to suggest when don't know about the problem!
Henry Chua
Super Advisor

Re: usinf sed

Hi guys,

Thanks for the input, my case is alittle more troublesome, as the I only want the data between W and the 1, and as the number of characters for the data differs from time to time it may be alittle difficult to achieve. I have tried using a script to do it.. but its really bulky.. do u guys have any idea?


Thank u!!
Gordon  Morrison_1
Regular Advisor

Re: usinf sed

Hi Henry,
If you just want everything between (but not including) the fiorst character on the line, and the last character before the first dot, then:

str1="WJV82LV83A1.ICZ34K.ws"
tmp=${str1#?}
echo $tmp
JV82LV83A1.ICZ34K.ws
str2=${tmp%%?.*}
echo $str2
JV82LV83A
What does this button do?
Hein van den Heuvel
Honored Contributor

Re: usinf sed

Hmmm, maybe there is something you are stil not specifying, as at first observation all posted suggestion would work, and would allow for a variable match count.

Here is yest another awk solution:

echo WJV82LV83A1.ICZ34K.ws | awk '{print substr($0,2,index($0,".")-2)}'

Or if the W is not not really only the first character as sugggested, but the fist W then:

echo WJV82LV83A1.ICZ34K.ws | awk '{w=index($0,"W")+1;print substr($0,w,index($0,".")-w)}'

With perl, you don't get the 'newline'.
It could look like:

echo WJV82LV83A1.ICZ34K.ws | perl -ne 'print $1 if (/W([^.]+)/)'

the ERE says: When you see a W, start to remember "(" one or more "+" non-periods "^."

In this case it looks like you could also write:
echo WJV82LV83A1.ICZ34K.ws | perl -ne 'print $1 if (/W([A-Z0-9]+)/)'

Here the ERE says: one or more characters from the A-Z range or 0-9 range.

With a perl solution you cn often do much more of the final processing all rigt there in perl.

Anyway... what specification detail are we missing?

Cheers,
Hein.


Henry Chua
Super Advisor

Re: usinf sed

Thanks for the help Guys.. Hello Hein, actually, what I meant was to capture "JV82LV83A" out of "WJV82LV83A1.ICZ34K.ws".. I have tried the suggestion and it gives "JV82LV83A1" instead, is there anyway I can get rid of the 1? .. "JV82LV83A" is the data i wanted, and as there are no fix pattern or number to it, I could not just cut out the string... can this be done?

sorry for the trouble..

thank u!!
Henry
Olivier LEGRAND
Frequent Advisor
Solution

Re: usinf sed

Hi
echo "WJV82LV83A1.ICZ34K.ws"|awk -F"." '{print $1}'|cut -c2-|sed s/.$//

Best regards
Olivier
Henry Chua
Super Advisor

Re: usinf sed

thanks Olivier, thats wat I am lookin for!!

thanks guys!!