awk- reading input file twice


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk- reading input file twice
# 1  
Old 04-26-2011
awk- reading input file twice

Hello,

I've been trying to come up with a solution for the following problem; I have an input file with two columns and I want to print as an output the first column without any changes but for the second column, I want to divide it by its last value. Example input:

1 9
2 10
3 11
4 12
5 13
6 14

Desired output:

1 9/14
2 10/14
3 11/14
4 12/14
5 13/14
6 14/14


So I don't really know how to read the file once in order to get the last value of the second column and then read it once again in order to print both columns, the second one divided by this last value.

Thank you for the help!
# 2  
Old 04-26-2011
Try this,
Code:
 awk -v lst_col=`awk 'END {print $2}' inputfile` '{print $1 FS $2"/"lst_col}' inputfile

# 3  
Old 04-26-2011
Code:
awk 'NR==FNR{s=$2;next}{print $1, $2/s}' file file

# 4  
Old 04-26-2011
I'm not too sure how to pass the file as "inputfile" because I'm using a pipe from a previous awk result as an input ...so I'm basically doing a whole bunch of awks and this would be the last one:

Code:
 ... | sort | awk '{print $1, p += $2;}'| (this awk) > $file.result



# 5  
Old 04-26-2011
Code:
... | sort | 
awk '{a[++c]=$1; b[c]=$2}END{for(i=1;i<=c;i++)print a[i], b[i]/b[c]}'

Edit: should be:

Code:
... | sort | 
awk '{a[++c]=$1; b[c]+=$2}END{for(i=1;i<=c;i++)print a[i], b[i]/b[c]}'


Last edited by Franklin52; 04-26-2011 at 05:10 AM.. Reason: Typo
This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 04-26-2011
Quote:
Originally Posted by Franklin52
Code:
... | sort | 
awk '{a[++c]=$1; b[c]=$2}END{for(i=1;i<=c;i++)print a[i], b[i]/b[c]}'

Edit: should be:

Code:
... | sort |

Quote:
Originally Posted by Franklin52
Code:

awk '{a[++c]=$1; b[c]+=$2}END{for(i=1;i<=c;i++)print a[i], b[i]/b[c]}'




I'm a bit confused... Is this only for the last awk? or does it include the previous one as well?

Code:
awk '{print $1, p += $2}'

# 7  
Old 04-26-2011
Quote:
Originally Posted by acsg



I'm a bit confused... Is this only for the last awk? or does it include the previous one as well?

Code:
awk '{print $1, p += $2}'

After the sort command this command should be suffice:
Code:
... | sort | 
awk '{a[++c]=$1; b[c]+=$2}END{for(i=1;i<=c;i++)print a[i], b[i]/b[c]}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. UNIX for Dummies Questions & Answers

User input while reading from a file

I am not able to capture the user input in this script(bash).There is prompt for user input.Could some one help me capture user input while reading afile? while read line do echo "$i" path1=$line path2=`echo $line|sed s/new_dir/old_dir/` echo "Do you want to replace?";... (4 Replies)
Discussion started by: parijat guh
4 Replies

3. Shell Programming and Scripting

Help with reading two input files in awk

Hello, I'm trying to write an awk program that reads two files inputs. example, file 1: 0.00017835 0.000176738 0.00018811 0.000189504 0.000188155 0.000180065 0.000178991 0.000178252 0.000182513 file 2: 1.7871769E-05 1.5139576E-16 1.5140196E-16 1.5139874E-16 1.7827407E-04 ... (5 Replies)
Discussion started by: joseamck
5 Replies

4. UNIX for Dummies Questions & Answers

Help in reading the date from the input file name

Hi, I need to read the date from the input file. The format of the input file is as follows: a_b_c_yyyymmdd.txt I need to read the date(yyyymmdd) part from the name of the input file. Would really appreciate if someone can help me in this regard Thanks a lot. (1 Reply)
Discussion started by: Sunny_teotia
1 Replies

5. Shell Programming and Scripting

awk script - reading input lines

Can I do something like, if($0==/^int.*$/) { print "Declaration" } for an input like: int a=5; If the syntax is right, it is not working for me, but I am not sure about the syntax. Please help. Thanks, Prasanna (1 Reply)
Discussion started by: prasanna1157
1 Replies

6. Shell Programming and Scripting

Reading from standard input with awk

Hello, Could somebody please give me an awk example on how to read from the standard input. It means as the "read" function in Korn shell. Thx in advance ... (3 Replies)
Discussion started by: rany1
3 Replies

7. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

8. Shell Programming and Scripting

reading input from a file

I am trying to read input for a C program (that expects input from the user) from a file using the shell command: progname < filename but it seems that the program considers the char '<' as the first input, hence causing an "error" in my program. I checked it with another program and it... (2 Replies)
Discussion started by: nadbar
2 Replies

9. Shell Programming and Scripting

Reading an Input file and searching for occurrences WIHOUT SED or AWK

Hi people. I am new to shell scripting, so I need a little help. I want to create a script named that takes an argument as a file, Read the input file and look for occurrences of the current username (say abc.xyz) who is executing the script. On finding an occurrence of the username take that line... (2 Replies)
Discussion started by: kartikkumar84@g
2 Replies

10. Shell Programming and Scripting

awk reading 2 input files but not getting expected value

I'm reading 2 input files but not getting expected value. I should get an alpha value on file_1_data but not getting any. Please help. >cat test6.sh awk ' FILENAME==ARGV { file_1_data=$0; print "----- 1 Line " NR " -----" $1; next } FILENAME==ARGV { file_2_data=$0; print "----- 2... (1 Reply)
Discussion started by: pdtak
1 Replies
Login or Register to Ask a Question