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
How to compare a field value of a same column? gobinath Shell Programming and Scripting 6 05-06-2009 01:39 AM
Compare 2 files through multiple fields newinawk Shell Programming and Scripting 4 06-12-2008 04:34 PM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
awk compare column between 2 files phamp008 Shell Programming and Scripting 3 01-18-2008 12:24 AM
Compare two arrays in sh or compare two fields rijeshpp Shell Programming and Scripting 0 10-31-2007 02:47 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 06-19-2008
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
How to read and compare multiple fields in a column at the same time

Hi,

Currently I am coding up a nasty way of reading file input using *cat* rather than *read*. My text input looks like

TextA 100
TextB 110
TextC 120

Currently I am using cat |while read line to read the first column and second column fields.

cat foo.txt|while read line
do

myfirstcol=`echo "${LINE}"| awk '{print $1}'`

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

However, I would like to implement more intelligent code where there can be more than one cases in the second field.

TextA 100,101
TextB 110,143,155,160
TextC 120,200

and from reading the possiblities of second column, i wanted to choose the smallest grep value.

e.g
mysecondcol=`echo "${LINE}"|awk '{print $2}'`

#return the position
grep -n mysecondcol$ fileA.txt

Basically if by grabbing 100 return position 30 from grep,
and grabbing 101 return position 1 from grep., it would choose the
smallest which is 101 (since it return 1).

I am not too sure how can i implement the above logic by using *read data*

Please advise. Thanks.
  #2 (permalink)  
Old 06-19-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Code:
while read record 
do
     set -A recarray $( echo "$record" | tr -s ',' ' ')
# at this point you now have an array - ${recarray[0]} is column 1, the rest of the
# elements came from column 2
done < inputfile
in bash: declare -a recarray
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 10:42 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