I run a command to reformat my textfile:

awk -F, '{if ($6<0) print $1$2,"   "$3$4,$5$6;
               else print $1$2,"   "$3$4$5,$6;}' filename.txt > seqXXX.txt

How can I make this into a script, so I don't have to type the command every time? I read some articles on how to do it, but got more confused

  • 2
    Welcome to StackExchange. Please consider accepting Kusalananda's answer if it solves your issue as represented in your original post. You accept an answer by checking the green mark to its left. Accepting an answer signals to others who browse the forum that the answer is satisfactory. – Cbhihe Dec 22 '19 at 11:12

How to do this depends somewhat on what part of the command you want to be turned into a script. I'm giving you three variations below:

  1. The awk program itself as a script.
  2. The command as a whole as a script.
  3. The command as shell function.

The awk program could be turned into a script that looks like

#!/usr/bin/awk -f

BEGIN { FS = "," }

    if ($6 < 0)
        print $1$2,"   "$3$4,$5$6
        print $1$2,"   "$3$4$5,$6

This first sets the field delimiter to a comma and then runs the block of code on each line in the input.

The #!-line used here makes it so that when you run the script file, it will be executed as if you had written awk -f script.awk on the command line. The path used on the first line should be adjusted to point to the correct awk interpreter on your system (see the output of command -v awk).

After making this file executable with chmod +x script.awk, it would be used as

$ ./script.awk filename.txt >seqXXX.txt

The command could be put into a shell script like so:


awk -F, '{if ($6<0) print $1$2,"   "$3$4,$5$6;
               else print $1$2,"   "$3$4$5,$6;}' "$@"

Here I've basically just transplanted the command into a shell script. The "$@" bit at the end of the awk command will be replaced by all command line arguments that you give to the script (probably just the name of your single input file).

After making this executable, you would use it as

$ ./script.sh filename.txt >seqXXX.txt

If the filenames are static and will never change, put these into the script too:


awk -F, '{if ($6<0) print $1$2,"   "$3$4,$5$6;
               else print $1$2,"   "$3$4$5,$6;}' filename.txt >seqXXX.txt

And then call your script without any extra arguments or redirections on the command line.

You could turn your command into a shell function. This is almost like an alias, but generally a bit more flexible:

myreformat () {
    awk -F, '{if ($6<0) print $1$2,"   "$3$4,$5$6;
                   else print $1$2,"   "$3$4$5,$6;}' "$@"

This would go wherever you usually place shell aliases etc. (maybe in ~/.bashrc if you're using the bash shell). The function would be available in the next shell session that you start.

It would be invoked in pretty much the same was as the shell script variant above:

$ myreformat filename.txt >seqXXX.txt
| improve this answer | |
  • We have here a beautiful example of why file extensions are a bad idea: 3 implementations and 3 file-names. If you were to change implementation and thus file-name, you would have to change every script that used it. However if you had no file-name-extension, then the name would not change, and all scripts that use it would continue to work. – ctrl-alt-delor Dec 22 '19 at 16:48
  • @ctrl-alt-delor Although this question isn't about filename suffixes, my main reason for using them is to avoid name clashes with pre-existing things, such as script (in this particular example) or test etc. They offer a sort of "name space" for personal scripts. Scripts installed system-wide is another matter. – Kusalananda Dec 22 '19 at 16:51
  • That is why . should never be in your PATH. If you are putting them into one of the bins of as a function, then yes have a name space, but be honest about it ( e.g. my-script). – ctrl-alt-delor Dec 22 '19 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.