The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extract field of characters after a specific pattern - using UNIX shell script jansat HP-UX 2 05-28-2008 12:08 AM
need to extract field of characters in a line jansat Shell Programming and Scripting 3 05-22-2008 09:44 AM
How to extract field from variable more efective pp56825 Shell Programming and Scripting 11 01-19-2008 10:34 AM
Extract Part of string from 3rd field $3 using AWK stakuri Shell Programming and Scripting 4 10-10-2007 12:28 PM
Trying to extract a field from /etc/passwd file.. xBuRnTx UNIX for Dummies Questions & Answers 2 10-05-2005 02:45 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-12-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
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 (permalink)  
Old 12-12-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by ahjiefreak View Post
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 (permalink)  
Old 12-12-2007
Zelp Zelp is offline
Registered User
  
 

Join Date: Jun 2005
Location: Champaign, IL
Posts: 19
do you have sample files?

Are you sure i and j are numbers?
  #4 (permalink)  
Old 12-12-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
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 (permalink)  
Old 12-12-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by ahjiefreak View Post
Please advise what is the best solution for this.
Did you not see my

Code:
q=`echo $i + $j | bc`
?
  #6 (permalink)  
Old 12-12-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Yeap porter,
But my computation is complex which is

$j/$i+$j.

Please advise. Thanks.

-Jason
  #7 (permalink)  
Old 12-12-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Code:
j=`echo \( $j / $i \) + $j | bc`
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0