0

I'm new to scripting and need a lot of help understanding how to load in a text file that will be passed in by a parameter. I'm afraid that you'll have to really dumb down your answers. Please explain like I am a 10 year old!

For example, how does one write a script that accepts two parameters, a file path and an integer.

Also how do you run the script? would you just call script.sh -f data.txt

1
  • 2
    If you need a "lot of help", you need to read the manual. If you have a more specific problem, StackOverflow would be a useful place to ask for help. Commented Apr 29, 2012 at 18:24

3 Answers 3

1

Could you explain your requirement a bit in detail?

I assume you are looking to extract the Nth column from a given file. You don't need to parse command-line parameters within AWK, instead just pass them from sh(1) itself.

# foo.sh
awk "{ print $"$1" }" $2

Here I'm escaping out of AWK to get the first argument which is the position number. If you find that too confusing you can manipulate ARGC, ARGV. Read awk(1).

Hope that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, thanks for your help. I am trying to write a script that accepts an integer, N, and extracts the Nth column from a text file that is also passed in. The part i'm specifically stuck on at the moment is i want to write a script that recognises getopts such as -f and -s. Do you have any advice on how to do this?
@user1136076 AFAIK AWK cannot do it portably, you can use getopt(1) and like I had mentioned earlier use sh(1) to pass around the params for awk(1).
You should use the -v option to pass shell variable values into AWK variables instead of embedding shell variables directly into an AWK script.
0

You can do this by manipulating ARGC and ARGV to extract your integer from the argument list in the BEGIN block. You can put your script in a text file with the first line being #!/usr/bin/awk -f and then make the file executable with chmod a+x.

Comments

0

This page of the GNU AWK (gawk) manual describes how to process options as getopt would.

Include the function shown on that page in your script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.