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