awk script to print file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to print file name
# 1  
Old 12-15-2015
awk script to print file name

I have the following awk script that looks at the first 2 columns of multiple files and when they match, it prints the min of the 3rd column.

Code:
for year in tave-{1950..2015}.txt
do
cat "$year"
done |
awk '{n=$3; $3=x} !($0 in A) || n<A[$0] {A[$0]=n} END{for(i in A) print i A[i]}' > output.txt



Code:
File 1

24.01  -81.01    1.0
24.02  -81.02    1.0
24.03  -81.03    3.0

File 2

24.01  -81.01    5.0
24.02  -81.02    3.0
24.03  -81.03    1.0

File 3

24.01  -81.01   -1.0
24.02  -81.02    5.0
24.03  -81.03    0.0

Current output

24.01  -81.01   -1.0
24.02  -81.02    1.0
24.03  -81.03    0.0


I need the output file to print the first 2 columns and the $year of the min value in the 3rd column.

Thanks in advance!
# 2  
Old 12-15-2015
Try:
Code:
for year in tave-{1950..2015}.txt
do
  echo "$year"
  cat "$year"
done | 
awk 'NF==1{y=$1; next} {n=$3; $3=x} !($0 in A) || n<A[$0] {A[$0]=n; Y[$0]=y} END{for(i in A) print i Y[i]}'


Last edited by Scrutinizer; 12-15-2015 at 02:45 PM..
# 3  
Old 12-15-2015
Not sure I understand. Your loop wouldn't find any of your sample files, and "the $year of the min value in the 3rd column" is far from clear. ($year would be sth like "tave-2015.txt")
Please post some more meaningful samples.
# 4  
Old 12-16-2015
Everything in awk:
Code:
awk '
{n=$3; $3=""}
!($0 in A) || n<A[$0] {A[$0]=n; F[$0]=FILENAME}
END {for(i in A) print i A[i], F[i]}
' tave-{1950..2015}.txt

This prints a 4. column.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

awk print in script

Hi, Thanks to RudiC I have a functioning awk portion of a script which reads a text file and replaces all matching values in an XML. I need help with placing print statements in the script to see line by line what is being replaced. Script: #!/bin/sh if then echo else ... (7 Replies)
Discussion started by: ocbit
7 Replies

3. Shell Programming and Scripting

awk script to match and print

I need a script that will search for a string from column 1 in file A and when the string matches the last column in file B, print columns 1, 2 (file A) and columns 2, 3 (file B). input file A stringtomatch1 a stringtomatch2 a stringtomatch3 b file B junkcolumn1 printcolumn2... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

4. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

5. Shell Programming and Scripting

awk based script to print the "mode(statistics term)" for each column in a data file

Hi All, Thanks all for the continued support so far. Today, I need to find the most occurring string/number(also called mode in statistics terminology) for each column in a data file (.csv type). For one column of data(1.txt) like below Sample 1 2 2 3 4 1 1 1 2 I can find the mode... (6 Replies)
Discussion started by: ks_reddy
6 Replies

6. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

7. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

8. Shell Programming and Scripting

awk script: print line number n of another file

Hi, I wrote an awk script to analyse file A. I call the script with files A and B. File A has lines like: 000000033100001 000000036100001 000000039100001 The first 9 characters are interpreted as a line number; for each line number found I want to output this line number of file B. ... (13 Replies)
Discussion started by: kpg
13 Replies

9. Shell Programming and Scripting

awk/shell script to print each line to a file

Dear People, My query is: have a file, which looks likes this: 10 20 30 40 50 1 2 3 4 5 100 200 300 400 500 what i need is: "PRINT EACH LINE TO AN UNIQUE FILE" desired output: file 1 10 20 30 40 50 file 2 1 2 3 4 5 (3 Replies)
Discussion started by: saint2006
3 Replies

10. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies
Login or Register to Ask a Question