bash - Shuffle piped through sed gives different number of lines -
i have data file thousands of lines, each consisting of 5 numbers. example:
23 31 56 21 34 34 76 34 75 32 ... ...
i want write bash script select n% lines @ random, , output them last entry set 0. remainder of entries want output line is. don't care in order lines output.
my attempt @ doing shuffle file, take first n% of lines , use awk
print them 0 in last place. output remainder of lines. here attempt:
#! /bin/bash number=$2 numlines=$(less $1 | wc -l) number=$(echo $number'*'$numlines | bc) number=$(echo $number'/'100 | bc) shuffledfile=$(less $1 | shuf) # following line echos shuffled file, gets first $number lines, , prints them 0 in final column echo "$shuffledfile" | sed -n --unbuffered "1,/$number/p" | awk '{print $1" "$2-7200" "$3" "$4" 0"}' echo "$shuffledfile" | sed -n "/${number}/,/${numlines}/p" | awk '{print $1" "$2" "$3" "$4" "$5}'
my problem each time run script different number of lines output. have determined if don't shuffle file, works expected. in advance.
you using wrong notation printing lines sed
, should be:
sed -n 'fromline,toline p'
currently printing line 1 whichever line contains /$number/
, or in second case first line containing /${number}/
following line containing /${numlines}/
which, random input, rather unpredictable.
Comments
Post a Comment