want to print output if u have rows and columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting want to print output if u have rows and columns
# 1  
Old 04-21-2009
want to print output if u have rows and columns

U have a file
File 1
0.3 0.2 0.4 0.3 0.8 0.11 0.22
0.4 0.23 0.45 0.56 0.78 0.98
0.11 0.32 0.2 0.4 0.45 0.54
0.2 0.33 0.44 0.21 0.22 0.98
0.8 0.2 0.34 0.54 0.98 0.12
0.1 0.22 0.32 0.34 0.89 0.22


File 2
rows columns
3 1
2 3
4 2
5 5

so from file 2 into file 1
output will be
0.11
0.45
0.2
0.89

I have used using awk

awk -v num=3 '{if(NR==num) print($0);}' File1 | awk '{print $1}'

awk -v num=2 '{if(NR==num) print($0);}' File1 | awk '{print $3}

Now problem is it is long manual process as it is having big file

So any done using perl or shell script..????

Last edited by cdfd123; 04-23-2009 at 01:50 AM.. Reason: check in file 1
# 2  
Old 04-21-2009
Code:
awk '{ printf "NR == %d { print $%d }\n", $1, $2 }' File2 |
 awk -f - File1

# 3  
Old 04-21-2009
Nice one Smilie

And another one, assuming typos in your example output:
(use gawk, nawk or /usr/xpg4/bin/awk on Solaris)
Code:
awk 'NR == FNR { _[$1] = $2; next }
FNR in _ { print $_[FNR] }' File2 File1

# 4  
Old 04-23-2009
Java some awk error??

Thanks for the reply
But after running the program the output is
0.45
0.11
0.33
0.98

But is shud be
0.11
0.45

0.33
0.98
# 5  
Old 04-23-2009
Would you mind to explain the logic behind?
# 6  
Old 04-23-2009
Try...
Code:
awk 'FNR==NR{for(i=1;i<=NF;i++)a[NR,i]=$i;next}{print a[$1,$2]}' file1 file2

...gives...
0.11
0.45
0.33
0.98
# 7  
Old 04-23-2009
output in order wise

Quote:
Originally Posted by radoulov
Would you mind to explain the logic behind?
Dear Radulov,
actually overall output was right in using this awk command but in random order as u can see in output
first one came as second order and second came as first as a large file is there it is confusing where it is ? But command wise is right

Another latest reply is giving right answer in orderwise

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert rows to column and print output in required format

Hi All, i am trying to print the solaris 11 packages in below required format, But i am unable to do that. Current ouput : root@abc# pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' NAME: QLogic 570x/571x Gigabit Ethernet Driver VERSION: 11.11,REV=2009.11.11 ... (7 Replies)
Discussion started by: balu1234
7 Replies

2. UNIX for Dummies Questions & Answers

Converting txt output to rows and columns and send report via mail.

Hi All, I would like to send below output in a tabular column ( xml or excel ) and send a mail. vinay unix anil sql vamsee java request to suggest a solution. (1 Reply)
Discussion started by: Girish19
1 Replies

3. Shell Programming and Scripting

Match two columns from two files and print output

Hello, I have two files which are of the following format File 1 which has two columns Protein_ID Substitution NP_997239 T53R NP_060668 V267M NP_058515 P856A NP_001206 T55M NP_006601 D371Y ... (2 Replies)
Discussion started by: nans
2 Replies

4. Shell Programming and Scripting

Print output as columns

Normal grep is not working to get the output. Sample Input: newjob: abc command name: a=b+c newjob: bbc command name: c=r+v newjob:ddc newjob:kkc command name: c=l+g newjob:mdc newjob:ldc newjob:kjc command name: u=dl+g newjob:lkdc newjob:lksdc command name: o=udl+g (6 Replies)
Discussion started by: onesuri
6 Replies

5. Shell Programming and Scripting

match two key columns in two files and print output (awk)

I have two files... file1 and file2. Where columns 1 and 2 of file1 match columns 1 and 2 of file2 I want to create a new file that is all file1 + columns 3 and 4 of file2 :b: Many thanks if you know how to do this.... :b: file1 31-101 106 0 92 31-101 106 29 ... (2 Replies)
Discussion started by: pelhabuan
2 Replies

6. Shell Programming and Scripting

Arrange output based on rows into columns

Hi All, I would like to ask help on how can i achieve below output. Inputfile: Oct11,apa1-daily,01:25:01 Oct11,apa2-daily,01:45:23 Oct12,apa1-daily,02:30:11 Oct12,apa2-daily,01:55:01 Oct13,apa1-off,01:43:34 Oct13,apa2-off,01:22:04 Desired output: Clients ... (3 Replies)
Discussion started by: mars101
3 Replies

7. Shell Programming and Scripting

sql output from multiple rows and columns as variables in a script

This is for an Oracle journal import. I was using a pl/sql package and oracle API's. Oracle added invoker rights to their API's and now my package won't run. I didn't want to use their API's anyway. The only reason i was using pl/sql and the API's (just a package) was to utilize a cursor. How... (2 Replies)
Discussion started by: lmu
2 Replies

8. UNIX for Advanced & Expert Users

Print rows into columns

Hi, I required to print all rows into columns(some comma separated or else) till a pattern match found. Using Solaris UNIX scripting my input is like abc def ghi jkl mno END pqr stu vwx yz 123 END ... ... (7 Replies)
Discussion started by: vikash.rastogi
7 Replies

9. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

10. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies
Login or Register to Ask a Question