-1

I have text file which contains numerous lines. I need to do:

  1. shuf txt.txt
  2. From shuf output read first line to a variable $line

How to represent it in one line for bash script?

Now it is like this:

shuf txt.txt -o aaa.txt
n=$(head -n 1 aaa.txt)
rm -rf aaa.txt

As you may notice, it is not very nice

2 Answers2

0

Sounds like homework. Here's some hints.

Assigning a variable to the result of a command:

x=`command`

Assignment with pipe:

x=`command1 | command2`

Assigning the first line of command1's output to a variable:

x=`command1 | head -n1`
oguz ismail
  • 1
  • 16
  • 47
  • 69
webb
  • 4,180
  • 1
  • 17
  • 26
0

You can easily do this with shuf:

n=$(shuf -n1 file)

https://www.gnu.org/software/coreutils/manual/html_node/shuf-invocation.html

Related question on stack overflow.com: What's an easy way to read random line from a file in Unix command line?

l'L'l
  • 44,951
  • 10
  • 95
  • 146