![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|