Categories
Mastering Development

xargs with head -n

I am trying to write a command which takes a random line from a CSV and outputs that line minus 5. Say, it outputs line 50, then it should print from 46 to 50 both included. I can achieve this in two steps, however when trying to use "xargs" to pass the line number to the following command (head -n) I got an error saying: "head: invalid number of lines: ‘{}’".
My command is:

cat test.csv |  awk '{ print NR }' | shuf -n 1 | xargs -l head -n "{}" test.csv | tail -n 5

Where "xargs -l head -n "{}" test.csv" should collect the number from the "shuf" function and then tail it to 5 to grab the last 5 rows.

What am I doing wrong?

Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *