How to extract elements in a field using a number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract elements in a field using a number
# 8  
Old 12-12-2007
Hi porter,

Still has error of:-
5
6

7
2
2

3
2
2

3
(standard_in) 1: parse error



My sample code is:-
#!/bin/bash

cat a.txt|while read LINE
do

char=`echo "${LINE}"| awk '{print $2}'`

#echo $char
i=`grep -n "^$char" b.txt|awk '{print $2}'`
j=`grep -n "^$char" b.txt|awk '{print $3}'`
k=`grep -n "^$char" c.txt|awk '{print $1}'`
q=`echo \( $j / $i \) + $j | bc`

echo $i
echo $j
echo $k
echo $q

done

My sample input file (let say for illustration purpose here) is

a.txt
aa 108
bb 109
cc 110
ddd 100

b.txt
108 5 6
110 2 2
109 2 2

c.txt

2 0.959341 109
3 0.959341 108
4 0.797717 367
5 0.788534 83


I dont see any problem in here though...please advise. Thanks.
# 9  
Old 12-12-2007
As always I recommend debugging scripts using

(a) strategically placed statements that print the value of variable

(b) using "#!/bin/sh -x" at the start of the script

(c) the equivalent of asserts, where you exit if some condition is not met, see (a)
# 10  
Old 12-12-2007
Hi Porter,

I debug the script and found out that:-

i) ++ echo '6/(' 5 '+6)'
++ bc
+ q=0

Does it means bc will round down the value? How can I get the exact value of division in this case?

ii) As I raised before, given the file has quite similar number.It will still print out all the possible number that found.

++ awk '{print $1}'
+ k='2:
3:'

This basically print the positions of file in c.txt(below) when 108 is grepped. Which means k=`grep -n "$char" c.txt|awk '{print $1}'` is not working to grab exact number.


2 0.959341 109
3 0.959341 108
4 0.797108 367
5 0.788534 83

iii)the (standard_in) 1: parse error comes in when the bc tries to calculate non value from (non match entry). Do you have any idea how to prevent the q to be calculate when the grep returns empty?
I tried to insert while and if inside but it returns me
./grep2.bash: line 49: syntax error near unexpected token `done'
./grep2.bash: line 49: `done '

Please help. Thanks.

-Jason
# 11  
Old 12-13-2007
Code:
select b.col2 i, b.col3 j, c.col1 k, (b.col3/b.col2)+b.col3 q
from a.txt a, b.txt b, c.txt c
where a.col2 = b.col1
and a.col2 = c.col3

See how simple & clean? Now you just need to upload these three files into three database tables Smilie

On a serious note, use FILENAME variable in awk to differentiate between multiple files. Using awk is much better than wrestling with shell constructs/commands - particularly if your files are big. The performance gains are massive.

HTH
# 12  
Old 12-13-2007
Quote:
Originally Posted by ahjiefreak
Do you have any idea how to prevent the q to be calculate when the grep returns empty?
"if/then/fi" springs to mind.

Regarding the calculations appearing like integer, try changing the formula to

Code:
j=`echo \( $j.00000 / $i.00000 \) + $j.00000 | bc`

# 13  
Old 12-13-2007
Hi,

Do you mean have this code in the bash itself?
I have error of

./grep4: line 3: syntax error near unexpected token `i,'
./grep4: line 3: `select b.col2 i, b.col3 j, c.col1 k, (b.col3/b.col2)+b.col3 q'


Rgrds,
Jason
# 14  
Old 12-13-2007
Computer

Sorry Jason - that was my lame try at humor - do not execute that Smilie

Try porter's solution & post the results.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

2. UNIX for Beginners Questions & Answers

How can we extract specific elements from XML?

Hi, I have a requirement to extract specific element value dynamically from XML message. Here is the sample message: <File> <List> <main> <dir>doc/store834/archive</dir> <count>5</count> </main> <main> <dir>doc/store834/extract</dir> <count>6</count> </main> <main> ... (3 Replies)
Discussion started by: renukeswar
3 Replies

3. Shell Programming and Scripting

Issue with the incorrect number of array elements

Hello , I have a file : RestartSession.txt with the below contents : Backup p203pcrw01_OS_Weekly Failed full 10/11/2015 10:00:07 PM 1444572007 10/11/2015 10:26:23 PM 1444573583 0:00 0:26 18.76 1 08 0 0 0 2 2 180668 ... (4 Replies)
Discussion started by: rahul2662
4 Replies

4. UNIX for Dummies Questions & Answers

Sort array elements from same field

Hi, input: line1|error_type_a@15 line1|error_type_c@10 line1|error_type_b@5 line2|error_type_f@3 line2|error_type_a@1 I would need to place all the second fields with common first field on the same line, BUT with sorted error position number: line1|error_type_b@5; error_type_c@10;... (5 Replies)
Discussion started by: beca123456
5 Replies

5. Shell Programming and Scripting

Extract only required elements from XML.

Hi , I have an XML like this. <Request> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <version>v44</version><messageId>7247308192</messageId><timeToLive>72000000000</timeToLive> </Request>. I want to extract on version and messageId. As in my output... (13 Replies)
Discussion started by: chetan.c
13 Replies

6. Shell Programming and Scripting

Get number of elements in a list

Hi all I would like to know the number of elements in a list. $list=`ls xyz*` I want to get the number of files xyz* in the folder. Anybody please help!!! (5 Replies)
Discussion started by: VidyaVenugopal
5 Replies

7. Shell Programming and Scripting

Sorting on two fields time field and number field

Hi, I have a file that has data in it that says 00:01:48.233 1212 00:01:56.233 345 00:09:01.221 5678 00:12:23.321 93444 The file has more line than this but i just wanted to put in a snippet to ask how I would get the highest number with time stamp into another file. So from the above... (2 Replies)
Discussion started by: pat4519
2 Replies

8. Shell Programming and Scripting

Number of elements in Word list

Hello everyone, can anyone let me know if there is a way to get the count of elements in a word list that I use for a for loop in the way: for single_result in $results ; do ....... I know I can increment a counter in my for loop, but would there be a way to know the total number of elements in... (4 Replies)
Discussion started by: gio001
4 Replies

9. Shell Programming and Scripting

How to extract elements using Awk

Hi, I have this typical extraction problem in AWK. I have 3 input files.. i) First one is somehow like an oracle of:- foo 12,23,24 bla 11,34 car 35 ii)Second file is basically detailing the score for each of the second field of first file. Besides, for the first column, it is the... (3 Replies)
Discussion started by: ahjiefreak
3 Replies

10. Shell Programming and Scripting

eval, array, number of elements - couldn't get it

I try to get a number of elements in an array, using dynamic array name. I need the array name to be dynamic. The array name is constructed as 'inf_ln_$nmb', where $nmb is a file line number So, say I have the arr 'inf_ln_4': > for (( el=0; el<${#inf_ln_4}; el++ )); do > echo "$el:... (1 Reply)
Discussion started by: alex_5161
1 Replies
Login or Register to Ask a Question