0

I have a text file like this:

first, second
hello, bye
one, two
good, bad
day, night

I would like to have a script which, based on a variable, retrieves the last word of a given line using grep or awk.

for example: if num is 3, output should be two.

It's not necessary to ask for num's value.

2 Answers 2

5

Using awk:

$ awk -v num="$num" 'NR == num { print $NF }' data.in

Testing it:

$ num=3
$ awk -v num="$num" 'NR == num { print $NF }' data.in
two

The awk script reads the input file record by record (a record is by default a line). Once it hits the record corresponding to the num variable, it prints the last field of that record (a field is by default a whitespace-separated column).

The num variable inside the awk script is an awk variable that we initialize to the value of the shell variable num with -v num="$num" on the command line.

NR is the current record number, and NF is the number of fields in this record. $NF is the data of the last field.

If your file is strictly comma-separated, add -F ',' to the command line:

$ awk -v num="$num" -F ',' 'NR == num { print $NF }' data.in

With grep you can't select a specific line, but together with sed you can filter out the line you want and then get the last bit after the last comma:

$ sed -n "${num}p" data.in | grep -o '[^,]*$'

The sed bit will get the specified line while the grep bit will extract everything after the last comma on that line.

You may do it with sed only too:

$ sed -n "${num}s/^.*,\(.*\)$/\1/p" data.in

Here, the substitution is applied to only the line whose number is $num, and it replaces the whole line with the contents of the line after the last comma and outputs the result. All other output is inhibited with the -n command line switch.

Alternatively, use a substitution in sed to simply delete everything on the line to the last comma:

$ sed -n "${num}s/^.*,//p" data.in
3
  • You don’t need grep with sed in your last example, sed can extract the last word too ;-). Commented Mar 14, 2017 at 15:56
  • @StephenKitt Of course it can (added it). Using sed for extracting bits of lines is a bit messy though... Commented Mar 14, 2017 at 16:03
  • 1
    I agree that extracting tends to get messy, but in this case you can just delete the part you don’t need (${num}s/^.*,//p). Commented Mar 14, 2017 at 16:11
1

In awk, NR is the line number, NF is the number of columns, so $NF is the last column. FS is the input record separator, ', ' for CSV without quoted commas should be enough.

awk -v FS=', ' 'NR==3{print $NF}'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.