3

I have written a simple awk command inside a file named as code.sh to find all the even numbers from the input file named as test and sort them numerically and display them on the command line

The input file test is

23
34
45
56
6
76
5
43
22
45
67
445
55
33
44
44
88
76
54
0
9
8
7
66

The code is written in code.sh is shown below

awk '
    BEGIN{
    }
    {
        if($1 % 2 == 0){
            print($1);
        }
    }
    END{
    }
' test | sort -n

In the command prompt I am executing the below code to run the script

./code.sh

The above code works perfectly fine and I am getting the expected output.

But in the code I am providing the name of the input file (in this case input file named test) inside the code.

But what I want is to provide the input file name in the command line.

And I think this is one of the solution.

BEGIN{
}
{
    if($1 % 2 == 0){
        print($1);
    }
}
END{
}

And in command line the code that I execute to perform the task is

awk -f code.sh test | sort -n

But in this case I have to write the sort command outside the script, in the command line. But I want to write all the code (the awk command to only select the even numbers and sort command using pipe | to sort the output of awk script) inside the code.sh file and provide the input file test in the command line argument.

IN HackerRank I HAVE EXECUTED THE BELOW CODE WHICH IS ACTUALLY MY REQUIREMENT AND, USED THE CUSTOM INPUT AND RUN THE SCRIPT, AND I AM GETTING THE EXPECTED OUTPUT. BUT I DON'T KNOW WHAT COMMAND THE HackerRank PLATFORM IS USING TO RUN THE SCRIPT

awk '
BEGIN{
}
{
    if($1 % 2 == 0){
        print($1);
    }
}
END{
} ' | sort -n

Now I want to execute the above script with providing the input file test in the command line in my own machine.

This command I tried which failed

./code.sh test
1
  • Sounds like you should tag your question with "hackerrank" if such a tag exists or post your question on a forum about "hackerrank" or contact whoever supports whatever that is. Commented Sep 7, 2020 at 14:45

4 Answers 4

3

You should make use of $1 in your script and make sure you pass it to awk script as Input_file. Something like following should do the trick(because of lack of expected output samples didn't test it).

Fixes in OP's code:

  • Also as an improvement I have removed BEGIN and END sections from OP's code since there were nothing happening in those section of OP's code.
  • Then to find the even lines one should use FNR%2==0 condition, where FNR represents the current line number.

cat script.ksh
val=$1
awk '
{
  if(FNR % 2 == 0){
    print($1);
  }
}
' "$val" | sort -n

Following is how one should run it:

./script.ksh Input_file

NOTE: You could also add a usage option to above script(which tells people in case they forget to pass any variable they should know how it actually runs), leaving it as an excessive for OP here.

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

2 Comments

or allow for multiple file inputs with awk '....' $@ | sort -n. Good luck to all.
2

Put this in code.sh:

#!/usr/bin/env bash

awk '
    script
' "${@:--}" | sort

The "${@:--}" syntax is so it'll read input from a file or a pipe. Obviously replace script with your actual awk script.

Comments

1

If you use GNU awk, you don't need sort:

# evens.awk
$1 % 2 == 0 {evens[NR] = $1}
END {
    PROCINFO["sorted_in"] = "@val_num_asc"
    for (i in evens)
        print evens[i]
}

then

gawk -f evens.awk test

The line PROCINFO["sorted_in"] = "@val_num_asc" instructs awk to sort the array numerically by the array values while iterating with for.

Refs:

Comments

1

you can embed sorting in the awk script as well

awk '!($1%2){print | "sort -n"}' "$1"

Comments

Your Answer

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