LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-11-2014, 11:56 PM   #1
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,407

Rep: Reputation: 141Reputation: 141
awk command in a shell script with a user-defined function


As you can see from the following, I'm trying to combine two different things, and I don't know how to even get started. What I need to do is run the awk command, which needs to run the function f() which is listed above it, which uses a control variable listed above that. The function is a phase rotation function, not that that contributes anything to the question. How do I combine these into a working script; whether shell or awk or perl or whatever? I've never had enough need for scripting to learn anything other than simple shell stuff.

Code:
#!/bin/sh
offset=72
function f(x) { x > $offset ? x-$offset-1 : x+100-$offset-1}

awk '{print f($17)" "strtonum($14)}'  fll.dat | sort -g | uniq  > phase.dat
 
Old 04-12-2014, 08:58 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
You just put all of the awk statements within the program text on the command line:
Code:
awk 'BEGIN { offset = 72 }
     function f(x) { return (x > offset ? x-offset-1 : x+100-offset-1) }
     { print f($17)" "strtonum($14) }' fll.dat | sort -g | uniq  > phase.dat
I've broken up the awk statements on multiple lines for readability. You can leave them that way, or smoosh them all together on one line -- your choice, makes no difference.

You could also pass that offset value in as a parameter rather than setting it in the BEGIN block. That way it could be a shell variable:
Code:
#!/bin/bash
Offset=72
awk -v offset=$Offset 'function f(x) { return (x > offset ? x-offset-1 : x+100-offset-1) }
       { print f($17)" "strtonum($14) }' fll.dat | sort -g | uniq  > phase.dat
 
1 members found this post helpful.
Old 04-12-2014, 09:20 AM   #3
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,407

Original Poster
Rep: Reputation: 141Reputation: 141
Thanks! I figured the function had to be part of the awk statement, but couldn't find any examples of how to do that. Easy once you showed me. I chose the second one using the bash variable "Offset". It worked first try. That gives me a lot to work with in future efforts, as well. Cheers!
 
  


Reply



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
User defined linux commands in a user defined shell Sushma05 Linux - Newbie 3 09-13-2013 07:21 AM
[bash][awk] command runs from shell, not from script JohnyDRipper Programming 2 03-09-2012 05:01 AM
user defined input in shell script ovaisalam Linux - Newbie 4 12-08-2010 06:44 AM
[SOLVED] Shell script using awk command gagou7 Linux - Newbie 6 11-05-2009 12:27 PM
user defined function in kernel vishalbutte Programming 4 02-07-2006 06:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:22 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
Open Source Consulting | Domain Registration