Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an awk command that will take a file named list.txt and only print the odd

ID: 3700311 • Letter: W

Question

Write an awk command that will take a file named list.txt and only print the odd numbered lines. (Do not put the awk code in a separate file, do it from the command line).

a. awk '(NR % 2)==1 {print}' list.txt

b. awk -odd '{print}' list.txt

c. awk 'select odd line {print}' list.txt

d. awk 'NR ~ odd {print}' list.txt

-------------------------------------------------------

Suppose the first field of each line is supposed to be an integer. Choose the an awk command that would print out an error message if that field is not an integer.

a. if ($1 !~ /^[0-9]+$/) print "error"

b. if ($1 != /^[0-9]+$/) print "error"

c. if ($1 != /[0-9]+/) print "error"

d. if ($field1 != number) print "error"

Explanation / Answer

Answer is as follows :

a) The syntax to print given scenario is

$ awk '{if (NR % 2 == 1) print $0}' file.txt

So similar to this syntax command is

awk '(NR % 2)==1 {print}' list.txt

i.e. option a

So the correct option is a

b) The correct option is b

Because the Not equal to operator is " != "

So we neglect option a is neglected and in option d we use number but not defined so it can be float or anny other type, So d is also neglect.

c option is wrong because we can't use $ sign in integer section that is syntax rule, we have to use that.

So the correct option is b

if there is any query please ask in comments...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote