Turn an awk one liner into a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Turn an awk one liner into a script?
# 1  
Old 01-04-2011
Turn an awk one liner into a script?

Hi,
I was just helped with an excellent one liner. Now I need to know how to turn it into a script that I can call at the command line. So, this works with file script:

------------
Code:
for file in DATA.txt;
do
        awk 'NR==1;NR>1{for (i=1;i<=NF;i++){a[i]+=$i}}END{for (i=1;i<=NF;i++){printf a[i]/(NR-1)"\t"};printf "\n\
"}' $file > TEST;
done

----------

so, i execute this by doing
Code:
./script

it works. but how do I modify things so I can do this script on any file, e.g.:
Code:
./script DATA.txt

I know that this is an easy one, but I just don't know what that is even called to search for an answer!!!

Thanks

Mikey

Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 01-04-2011 at 02:21 PM..
# 2  
Old 01-04-2011
Create the file (with an added check):
Code:
#! /usr/bin/awk -f

NR==1
NR>1{ for (i=1; i<=NF; i++) { a[i]+=$i; }
END { if (NR > 1) { for (i=1;i<=NF;i++) {printf a[i]/(NR-1) "\t";} } print ""; }

Make it executable:
Code:
chmod u+x scriptname

And run:
Code:
./scriptname inputfile... > outputfile

This User Gave Thanks to m.d.ludwig For This Post:
# 3  
Old 01-04-2011
Take a look at previous posts

https://www.unix.com/shell-programmin...ll-script.html

Essentially, the first parameter becomes $1 and the second becomes $2 and so on....
This User Gave Thanks to joeyg For This Post:
# 4  
Old 01-04-2011
THANK YOU AGAIN!

There was a missing curly bracket in there on the second line. I must be a real expert now Smilie


NR==1
NR>1{ for (i=1; i<=NF; i++) { a[i]+=$i; }}
END { if (NR > 1) { for (i=1;i<=NF;i++) {printf a[i]/(NR-1) "\t";} } print ""; }

And I this just a general way that you can do these things? I assume so!

Thanks again again,

Mike
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk one liner

The below code is a simple modified sample from a file with millions of lines containing hundreds of extra columns xxx="yyy" ... <app addr="1.2.3.4" rem="1000" type="aaa" srv="server1" usr="user1"/> <app usr="user2" srv="server2" rem="1001" type="aab" addr="1.2.3.5"/>What's the most efficient awk... (2 Replies)
Discussion started by: cabrao
2 Replies

2. Shell Programming and Scripting

Combine these two into one liner awk?

ignore the simplicity of the foo file, my actual file is much more hardcore but this should give you the jist of it. need to combine the two awks into one liner. essentially, need to return the value of one particular field in a file that has multiple comma separated fields. thanks guys cat foo... (1 Reply)
Discussion started by: jack.bauer
1 Replies

3. UNIX for Dummies Questions & Answers

awk or sed one liner

I have a data base of part numbers: AAA Thing1 BBB Thing2 CCC Thing3 File one is a list of part numbers: XXXX AAA234 XXXX BBB678 XXXX CCC2345 Is there a sed one-line that would compare a data base with and replace the part numbers so that the output looks like this? XXXX AAA234... (7 Replies)
Discussion started by: jimmyf
7 Replies

4. Shell Programming and Scripting

awk script does not work when written as "one-liner"

In my quest to solve a bigger problem (See my previous post called "Create SQL DML insert statements from file using AWK or similar" - sorry, not allowed to post urls until I have > 5 posts) I'm trying to get my head round awk, but have some problem figuring out why the following script does work... (2 Replies)
Discussion started by: Yagi Uda
2 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

7. Shell Programming and Scripting

Awk one-liner?

Hello, I have two files... File #1 1 3 2 5 File #2 3 5 3 1 3 7 9 1 5 2 5 8 3 3 1 I need to extract all lines from File #2 where the first two columns match each line of File #1. So in the example, the output would be: 1 3 7 2 5 8 Is there a quick one-liner that would... (4 Replies)
Discussion started by: palex
4 Replies

8. UNIX for Dummies Questions & Answers

need an awk one liner

example input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

9. UNIX for Dummies Questions & Answers

awk one liner

I need a one liner to" find /pattern/ print from x lines before "pattern" to y lines after "pattern" (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

10. Shell Programming and Scripting

awk one liner

input a 100 200 300 b 400 10 output a 100 a 200 a 300 b 400 b 10 Thanx (6 Replies)
Discussion started by: repinementer
6 Replies
Login or Register to Ask a Question