LINUX.ORG.RU

программа+bash


0

0

Приветствую, господа! Нужна на bash следующая прога: из текстового файла случайно выбирает фразу, разделение между фразами - перевод строки(Enter короче). Вот. Поделитесь опытом!

Вообщем заранее спасибо!

anonymous

Hi,

Try this. Not tested and should be treated only like primitive example of the idea.

#!/bin/bash

if [ $# -ne 1 ]; then echo "USAGE: `basename $0` txt_file" >&2 exit 1 fi

file="$1" total_lines=`wc -l "$file" | sed -e 's/^[ \t]*//; s/[^0-9]*$//'`

line=`awk -v total_lines=$total_lines ' BEGIN { srand( systime() ) r=rand() l=strtonum( sprintf( "%d", r * total_lines ) ) print l }'`

head -q -n "$line" "$file" | tail -q -n 1

sas
()
Ответ на: комментарий от sas

Sorry, Correct formating...

#!/bin/bash

if [ $# -ne 1 ]; then
echo "USAGE: `basename $0` txt_file" >&2
exit 1
fi

file="$1"
total_lines=`wc -l "$file" | sed -e 's/^[ \t]*//; s/[^0-9]*$//'`

line=`awk -v total_lines=$total_lines '
BEGIN {
srand( systime() )
r=rand()
l=strtonum( sprintf( "%d", r * total_lines ) )
print l
}'`

head -q -n "$line" "$file" | tail -q -n 1

# ------- end ---------------------------------

I hope this time formating will be ok

sas
()

тоже стало интересно:
sed -n $(( $RANDOM * `cat FileName.txt | wc -l` / 32767 ))p FileName.txt
или
sed -n $(( $RANDOM * `grep -c . FileName.txt` / 32767 ))p FileName.txt

Anonymous ★★★★★
()
Ответ на: комментарий от Anonymous

Спасибо, помогло ... тема закрыта!

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.