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
# 1  
Old 12-12-2007
How to extract elements in a field using a number

Hi,

I face difficulty where the number which I grep before I would like to use it as number to grep again in another file.

For example in file 1, I extract the second field and assign to variable "char" in a while loop. And then, I grep again this char to get i and j.

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}'`

I tried to use $i and $j for operation such that q=$i+$j; however, it gives me error.

Besides, I tried to grep again this "char" in another file using grep -n char$ file2 but it doesnt work. (In a typical example : grep -n element$ filename should give us the exact line of element)

Please advise. Thanks.

-Jason
# 2  
Old 12-12-2007
Quote:
Originally Posted by ahjiefreak
I tried to use $i and $j for operation such that q=$i+$j; however, it gives me error.
Do you want to add them numerically?

Code:
q=`echo $i + $j | bc`

or literally?

Code:
q=$i$j

# 3  
Old 12-12-2007
do you have sample files?

Are you sure i and j are numbers?
# 4  
Old 12-12-2007
Hi Zelp,

These are numbers. And basically in a while loop, I do not declare awk. Most of the time, I only do echo which in the end I could not use printf.

Sample code:-

#!/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}'`

echo $i
echo $j
echo $k


q=`echo (($j)/($i+$j))|bc `

echo $q

#awk '{
#q=$j/($i+$j)}

#END{

#printf("q is %d", $q);
#}'

done

I have error in echo where it doesnt allow me to perform computation. I am thinking to use AWK whihc is put easier but I could not because it invovles multiple files to read and compare.

Please advise what is the best solution for this. Thanks!


-Jason
# 5  
Old 12-12-2007
Quote:
Originally Posted by ahjiefreak
Please advise what is the best solution for this.
Did you not see my

Code:
q=`echo $i + $j | bc`

?
# 6  
Old 12-12-2007
Yeap porter,
But my computation is complex which is

$j/$i+$j.

Please advise. Thanks.

-Jason
# 7  
Old 12-12-2007
Code:
j=`echo \( $j / $i \) + $j | bc`

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