Skip to main content
added 128 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

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

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.

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
added 250 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

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.

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.

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.

added 337 characters in body
Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

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.

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

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.

Source Link
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k
Loading