extract a field from a long sentence!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract a field from a long sentence!
# 1  
Old 03-22-2011
extract a field from a long sentence!

Hi,

I want to extract the value of LENGTH column (high-lighted in red) from a file which will have several lines as shown below:

Code:
 
<INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="19" LENGTH ="15" PHYSICALOFFSET ="29" PICTURETEXT ="" PRECISION ="15" SCALE ="0" USAGE_FLAGS =""/>

I am reading the file in while loop and want to do something like this

Code:
 
while read line
do
var_len=`echo $line | awk -F\" '/^ *=/{print $2}' RS=LENGTH`
done < $file

But it outputs as
Code:
-bash: syntax error near unexpected token `('

Smilie

Your help would be much appreciated.

-dips
# 2  
Old 03-22-2011
If you just want the value you could use sed:

Code:
sed 's/.*LENGTH *="\([0-9]*\).*/\1/' file

Move the left bracket if you want to include LENGTH =
# 3  
Old 03-22-2011
This is working fine at my end. See below.
Code:
#cat $file
<INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="19" LENGTH ="15" PHYSICALOFFSET ="29" PICTURETEXT ="" PRECISION ="15" SCALE ="0" USAGE_FLAGS =""/>
#while read line; do var_len=`echo $line | awk -F\" '/^ *=/{print $2}' RS=LENGTH`; echo $var_len;done < $file
15
#awk -F\" '/^ *=/{print $2}' RS=LENGTH  $file
15

Please post the remaining code if you have.
# 4  
Old 03-22-2011
Code:
awk -F'[=" ]' '{for(x=y;x++<NF;){if($x=="LENGTH")print $(x+3)}}' file

# 5  
Old 03-23-2011
Hi,

Thanks for the responses!

Quote:
danmero - I cannot have a file as input to awk command. I want to use the command in the loop only. And that is why I am echoing it before providing it as input to awk.
Quote:
kato - Not necessarily the o/p I need would be of number type i.e. 0-9.
Quote:
pravin27 - I am really not sure how it is working at your end. The version I have is
Code:
Linux xxxxxxxx 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

May be it helps to resolve the mystery! Smilie

-dips

---------- Post updated 2011-03-23 at 12:08 PM ---------- Previous update was 2011-03-22 at 06:06 PM ----------

Hi,

The problem got resolved. It was a very silly mistake! Smilie

Code:
while read line
do
var_len=`echo "$line" | awk -F\" '/^ *=/{print $2}' RS=LENGTH`
done < $file

I had to wrap the $line variable in double quotes to get it working.

-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

SQL issues comparing Long field to sysdate

I am having hard time with this sql: select partition_name, high_value FROM user_tab_partitions WHERE table_name = 'WNP_TPRESPONSE_INSTANCE' and to_char(high_value) <= to_char(sysdate - 15, 'yyyymm') ; I get an error: ORA-00932: inconsistent datatypes: expected CHAR got LONG... (1 Reply)
Discussion started by: mrn6430
1 Replies

2. Shell Programming and Scripting

How to extract only relevant part from a sentence?

Hello All, I have a file with details such as below. How do i extract only the host and port ? eg: dbs.ads.com 1521 (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbs.ads.com)(PORT=1521))(CONNECT_DATA=(SID=vug)))... (5 Replies)
Discussion started by: JohnJacobChacko
5 Replies

3. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

4. Shell Programming and Scripting

Multiple long field separators

How do I use multiple field separators in awk? I know that if I use awk -F"", both a and b will be field separators. But what if I need two field separators that both are longer than one letter? If I want the field separators to be "ab" and "cd", I will not be able to use awk -F"". The ... (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

Extract a word from sentence

$SET_PARAMS='-param Run_Type_Parm=Month -param Portfolio_Parm="997" -param From_Date_Parm="2011-08-09"' Want to extract the value of "Portfolio_Parm" from $SET_PARAMS i.e in the above case "997" & assigned to new variable. The existence order of "Portfolio"Parm" can change, but the name... (2 Replies)
Discussion started by: SujeethP
2 Replies

6. Shell Programming and Scripting

extract from a long sentence

Hi, There's a long sentence from which I need to extract only the part which is at the right side of the word LENGTH i.e. 15 long sentence : <INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME... (3 Replies)
Discussion started by: dips_ag
3 Replies

7. Shell Programming and Scripting

Trim the sentence containing colon and period to extract a word in between

Hello All , i am a newbie in korn shell scripting trying to trim a sentence that is parsed into a variable . The format of the sentence has three words that are separated from other by a " : " colon and "." period . Format of the sentence looks like ... (5 Replies)
Discussion started by: venu
5 Replies

8. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

9. Shell Programming and Scripting

Trim leading zeros to make field 6 characters long

Hi all- I've got a file that will have multiple columns. In one column there will be a string that is 10 digits in length, but I need to trim the first four zeros to make it 6 characters? example: 0000001234 0000123456 0000234566 0000000321 output: 001234 123456 234566 000321 (5 Replies)
Discussion started by: Cailet
5 Replies

10. Shell Programming and Scripting

Subtract 100 from first field in long list? Simple manipulation?

It sounds so easy to do. I have a file thats laid out like this.. number text text text text (etc about 15 times with various text fields) I want to take the first field, "number", subtract 100 from it, and then put it back in the file. a simple little manipulation of the first field in... (4 Replies)
Discussion started by: LordJezo
4 Replies
Login or Register to Ask a Question