Help to get a second column value from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to get a second column value from a file
# 1  
Old 05-14-2009
Help to get a second column value from a file

Gurus,

I've very basic knowledge in Shell Scripting. I need a logic as per below.

I've a text file having 2 columns as ColA and ColB.

ColA|ColB
One|0
Two|225
Three|0
Four|500

Now, my requirement is "If value One is 0 then print OK else NO", "If value Two is 0 then print GO else NO" and so on.

How to write a program from the above condition? Can anyone please help me?

Thanks,
Venkat
# 2  
Old 05-14-2009
I am not really clear about your requirement. Perhapes you should give a sample inp/p and o/p corresponding to that i/p. What happenes both col a and colb are 0?

Hint for you.

Use | as delimeter and cut the fields - f1 and f2

Code:
while read line
do
col1=`echo $line | cut -d "|" -f1`
col2=`echo $line | cut -d "|" -f2`
if [ $col1 -eq 0 ]; then
echo "something"
else
echo "something"
fi
-- for col2 here--

done < file

we can use awk but I will let you go for this first...

Code:
awk -F "|" '{ if ($1 == 0)print "something" else print "something"
                  if ( $2 == 0) print somethng" else print "something"
               }' file

cheers,
Devaraj Takhellambam
# 3  
Old 05-14-2009
Quote:
Originally Posted by devtakh
I am not really clear about your requirement. Perhapes you should give a sample inp/p and o/p corresponding to that i/p. What happenes both col a and colb are 0?

Hint for you.

Use | as delimeter and cut the fields - f1 and f2

Why would you use cut instead of using the delimiter directly?
Quote:

Code:
while read line

Code:
while IFS=\| read col1 col2

Quote:
Code:
do
col1=`echo $line | cut -d "|" -f1`
col2=`echo $line | cut -d "|" -f2`
if [ $col1 -eq 0 ]; then
echo "something"
else
echo "something"
fi
-- for col2 here--

done < file

we can use awk but I will let you go for this first...

Code:
awk -F "|" '{ if ($1 == 0)print "something" else print "something"
                  if ( $2 == 0) print somethng" else print "something"
               }' file


Code:
awk -F\| '
$2 == "0" { print "NO" ; next }
{ print OK }
' file

# 4  
Old 05-14-2009
Code:
awk -F\| '{ print ($2 == "0") ? "NO" : "OK" }' file

# 5  
Old 05-14-2009
Quote:
Originally Posted by cfajohnson
Code:
while IFS=\| read col1 col2

just in case file is big, putting IFS outside of while loop will be beneficial.
# 6  
Old 05-14-2009
Quote:
Originally Posted by ghostdog74
just in case file is big,

If the file is large, you shouldn't be using a shell loop.
Quote:
putting IFS outside of while loop will be beneficial.

Putting IFS outside the loop has the disadvantage of changing it for commands inside the body of the loop as well as for the read command.
# 7  
Old 05-14-2009
Quote:
Originally Posted by cfajohnson

If the file is large, you shouldn't be using a shell loop.
maybe i should rephrase it. putting IFS inside the while loop(irregardless of file size), you are setting IFS every time it loops, am i right to say that? pls correct me if i am wrong.

Quote:

Putting IFS outside the loop has the disadvantage of changing it for commands inside the body of the loop as well as for the read command.
how about an example to show that... i am curious...as i seldom use the while loop like that. am i right to say , judging from your explanation, that IFS inside while loop has its own scope?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

3. Shell Programming and Scripting

[Solved] Sorting a column in a file based on a column in a second file

Hello, I have two files as the following: File1: F0100020 A G F0100030 A T F0100040 A G File2: F0100040 A G BTA-28763-no-rs 77.2692 F0100030 A T BTA-29334-no-rs 11.4989 F0100020 A G BTA-29515-no-rs 127.006 I want to sort the second file based on the... (6 Replies)
Discussion started by: Homa
6 Replies

4. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

5. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

6. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

7. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

8. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

9. Shell Programming and Scripting

To cut entire column from a file and apend it to another file as another column

file1.txt : india pakistan bangladesh japan canada africa USA srilanka Nepal file2.txt Delhi Tokyo washington I have to cut the first column of file1.txt and apend it with file2.txt as another column like this Delhi india Tokyo japan washington USA ... (4 Replies)
Discussion started by: sakthifire
4 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question