0

I have a text file

cat file.txt

This is line1
This is line2
This is line3
This is line4.

My requirement is to append Test1 ,Test2 to the lines randomly.

Output should be as below.

Test1 This is line1
Test2 This is line2
Test1 This is line3
Test2 This is line4.
fedorqui
  • 275,237
  • 103
  • 548
  • 598
user123
  • 21
  • 1
  • 3
    We are not here to do your job for you. You should make some attempts at solving the problem and then ask specific questions as to the problems you encounter. – Thom Apr 21 '15 at 12:14
  • You can see this question for better understanding: [link](http://stackoverflow.com/questions/10929453/bash-scripting-read-file-line-by-line) – bhansa Apr 21 '15 at 12:28

6 Answers6

2

I would use awk:

awk '{r=int(rand()*2)+1; $0="Text"r" "$0}1' input

int(rand()*2)+1 will return a random value between 1 and 2. $0="Text"r" "$0 adds Text[Random] at the start of each line. 1 is simply true and will lead to printing the modified line.

However this creates the same randomness across multiple invocations of it. Isn't this boring? If you want to have a different sequence of 1 and 2s in every invocation you need to specify a seed for the random generator, like this:

awk -vseed="$RANDOM" 'BEGIN{srand(seed)}{r=int(rand()*2)+1; $0="Text"r" "$0;}1' input

$RANDOM is a special bash variable which contains a different random number each time you access it. Using -v you can pass variables from the command line to awk. The awk function srand() sets the seed for the random generator.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
2

A bash solution using shuf:

while read line;do 
 echo "Test$(shuf -i 1-2 -n 1) ${line}"
done<infile
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
1

addToLine.sh

rand() {
    printf $((  $1 *  RANDOM  / 32767   ))
}
#rand_element borrowed from github.com/search?q=bashnative
rand_element () {
    local -a th=("$@")
    unset th[0]
    printf $'%s\n' "${th[$(($(rand "${#th[*]}")+1))]}"
}


sed -e 's/^/'"$(rand_element Test1 Test2)"' /' file.txt

Output:

Test1 This is line1
Test2 This is line2
Test1 This is line3

Learn more about sed

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
1

Based on hek2mgl's nice approach (+1 to him), let's first provide the set of words we want to add; then, use them randomly when printing:

awk -v text="Test1 Test2" '
       BEGIN {n=split(text, a)}
       {print a[int(rand()*n+1)], $0}' file

Test

$ awk -v text="Test1 Test2" 'BEGIN{n=split(text, a)} { print a[int(rand()*n+1)], $0}' a
Test1 This is line1
Test1 This is line2
Test2 This is line3
Test1 This is line4.

With more words:

$ awk -v text="Test1 Test2 Test3" 'BEGIN{n=split(text, a)} {print a[int(rand()*n+1)], $0}' a
Test1 This is line1
Test1 This is line2
Test3 This is line3
Test1 This is line4.
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

Write a program in some language with a loop that:

  • Writes Test1 or Test 2 to a file, with some kind of random() function
  • Reads the next line (read all characters until the '\n' character)
  • Write that line to the new file
moffeltje
  • 4,521
  • 4
  • 33
  • 57
0

You could also do a loop:

while read i ; do
  echo $i | sed "s/^/Test$(( ( RANDOM % 2 ) + 1)) /"
done < file.txt
Slizzered
  • 869
  • 2
  • 9
  • 23