How to read and compare multiple fields in a column at the same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read and compare multiple fields in a column at the same time
# 1  
Old 06-19-2008
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  
Old 06-19-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare values in multiple rows in one column using awk

I would like to compare values in column 8, and grep the ones where the different is > 1, columns 1 and 2 are the key for array. Every 4 rows the records values in columns 1 and 2 changed. Then, the comparison in the column 8 need to be done for the 4 rows everytime columns 1 and 2 changed ... (4 Replies)
Discussion started by: jiam912
4 Replies

2. UNIX for Beginners Questions & Answers

How to read multiple files at same time through UNIX scripting?

How to read multiple files at simultaneously? (1 Reply)
Discussion started by: Priyanka_M
1 Replies

3. Shell Programming and Scripting

UNIX append field with comparing fields from multiple column

I have a csv dump from sql server that needs to be converted so it can be feed to another program. I already sorted on field 1 but there are multiple columns with same field 1 where it needs to be compared against and if it is same then append field 5. i.e from ANG SJ,0,B,LC22,LC22(0) BAT... (2 Replies)
Discussion started by: nike27
2 Replies

4. Shell Programming and Scripting

Read multiple lines at a time from file

Hello All, I have a file like below.... dn: cn=user1,ou=org,o=org cn=user1 uid=user1 cn=user2,ou=org,o=org cn=user2 uid=user2 cn=user3,ou=org,o=org cn=user3 cn=user33 uid=user3 cn=user4,ou=org,o=org cn=user4 uid=user4 (6 Replies)
Discussion started by: s_linux
6 Replies

5. UNIX for Dummies Questions & Answers

Compare values of fields from same column with awk

Hi all ! If there is only one single value in a column (e.g. column 1 below), then return this value in the same output column. If there are several values in the same column (e.g. column 2 below), then return the different values separated by "," in the output. pipe-separated input: ... (11 Replies)
Discussion started by: lucasvs
11 Replies

6. Shell Programming and Scripting

Read multiple arguments in for loop each time

Hi, Guys I am new to shell programming and just get stuck with one simple question. please kindly help. According to the tutorial here, we can do something like for NODE in "ABC 10" "EFG 20" do set -- $NODE echo "letter is $1, number is $2" done And the result will... (3 Replies)
Discussion started by: yuanli
3 Replies

7. Shell Programming and Scripting

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (3 Replies)
Discussion started by: yerruhari
3 Replies

8. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

9. UNIX for Advanced & Expert Users

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

10. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies
Login or Register to Ask a Question