LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-21-2013, 12:15 PM   #1
tabbyagirl
LQ Newbie
 
Registered: Jul 2013
Distribution: Red Hat Enterprise Linux Client release 5.5 (Tikanga)
Posts: 7

Rep: Reputation: Disabled
turning awk command line entries into awk scripts


Hi guys,

I've been writing a little awk to do different stuff and learning alot from other guys and their examples but I only know how to run awk from command line entries, so I write them in an editor, copy and paste into a terminal and execute with a return.

I'd like to learn how (in general) to write an awk command line entry into awk scriptsy?

say a super simple command line like

[code] awk 'NR>1 {sum+=$1; ++n; print $1} END {print "average = " sum/n}' inputfile.dat [\code]

would get changed to what to run as a script?

many thanks, Tabby
 
Old 08-21-2013, 12:25 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You can either edit a file, e.g. script.awk by writing down only the awk code:
Code:
NR > 1 {
  sum += $1
  ++n
  print $1
}
END {
  print "average = " sum/n
}
and then run it using the awk command with option -f:
Code:
$ awk -f script.awk inputfile.dat
or you can write a script specifying awk as interpreter using the proper sha-bang at the beginning (note that the path must match the actual path of awk in your system):
Code:
#!/usr/bin/awk -f
NR > 1 {
  sum += $1
  ++n
  print $1
}
END {
  print "average = " sum/n
}
then make the script executable and run it directly:
Code:
$ chmod +x script.awk
$ ./script.awk inputfile.dat
Hope this helps.
 
Old 08-21-2013, 12:25 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Code:
#!/usr/bin/awk -f

NR>1 {
   sum+=$1

   ++n

   print $1
}

END { print "average = " sum/n }
Obviously change the shebang if your awk is located elsewhere

Too slow
 
Old 08-21-2013, 01:14 PM   #4
tabbyagirl
LQ Newbie
 
Registered: Jul 2013
Distribution: Red Hat Enterprise Linux Client release 5.5 (Tikanga)
Posts: 7

Original Poster
Rep: Reputation: Disabled
hey thanks guys!

it looks like I had a couple different methods intermixed, but that straightend them out and I'll use this as a reference if I get stuck again

Tabby
 
Old 08-22-2013, 01:18 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Please mark as SOLVED once you have a working solution.

Note: If you don't have a link already, you may find the following useful - bible (slight joke but it is my goto / bible)
 
Old 08-22-2013, 01:46 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
probably worth pointing out that you can use awk in a shell script
Example

Code:
#!/bin/bash
Input=/path/to/inputfile.dat

MeanSomeSum="$(awk 'NR>1 {sum+=$1; ++n; print $1} END {print sum/n}' $Input)"

if (( "$MeanSomeSum" -lt "2" )); then
    echo "$MeanSomeSum is less than 2"
else
    echo "$MeanSomeSum is not less than 2"
fi

awk 'NR>1 {sum+=$1; ++n; print $1} END {print "average = " sum/n}' $Input
Of course, you could do all that in awk, just an example
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED]Wierd AWK behavior / AWK not reading first line. Involar Linux - Newbie 9 11-28-2012 11:53 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 11:49 AM
AWK/BASH: get nth line from a file by getline feed to actions in a same awk line cristalp Programming 3 11-23-2011 12:38 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 06:39 PM


All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration