LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-12-2014, 12:56 AM   #1
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,409

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, 09:58 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,797

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
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, 10:20 AM   #3
Quakeboy02
Senior Member
 
Registered: Nov 2006
Distribution: Debian Linux 11 (Bullseye)
Posts: 3,409

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


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

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

All times are GMT -5. The time now is 02:54 AM.

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