Sed and random


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed and random
# 1  
Old 01-11-2010
Sed and random

Hi everyone, this is my first post.
Some time ago I did a script to convert .csv files to oracle inserts. Now I need to add something and I can't make it work. This is the code:
Code:
#!/bin/bash
#Rev. 2
paso_uno()
{
local tmp=`mktemp temp.uno`
cat $1 > $2
cat $2 > $tmp
mv $tmp $2
}
paso_dos()
{
local cnt=`wc -l $1 | cut -f1 -d" "`
local tmp=`mktemp temp.dos`
cnt=$((cnt - 1))
tail -n $cnt $1 > $tmp
mv $tmp $1
}
paso_tres()
{
local tmp=`mktemp temp.tres`
cat > $tmp <<-EOF
{print "INSERT INTO $2 (DATETIME, NAME, CODE) values(TO_DATE( '"\$1 ":segundos', 'DD/MM/YYYY HH24:MI:SS'), "\$2 ", "\$3 ");"}
EOF
cat $1 | awk -F, -f $tmp > inserts_$2.sql
rm $tmp
echo "" >> inserts_$2.sql
echo "commit;" >> inserts_$2.sql
}
main()
{
local tmp=`mktemp temp.datos`
paso_uno $1 $tmp
paso_dos $tmp
sed -i 's/,/./g' temp.datos
sed -i 's/;/,/g' temp.datos
paso_tres $tmp $2
rm $tmp
rnumber=$((RANDOM%49+10))
sed -i "s/segundos/$rnumber/g" inserts_$2.sql
}
usage()
{
echo "Sintaxis: `basename $0` archivo.csv ESQUEMA.TABLA"
exit 1
}
if [ $# != 2 ] ; then
usage
elif [ ! -f $1 ] ; then
echo "Error: No encuentro el archivo $1, 1/2 pila!" > /dev/stderr
usage
fi
main "$@"

The field DATETIME doesn't include seconds, so I'm trying to add them. What I did was to include the word "seconds" and then replace it with
Code:
rnumber=$((RANDOM%49+10))
sed -i "s/segundos/$rnumber/g" inserts_$2.sql

However this gives me the same number for all the lines! How can I get a different number for each line?

Thanks in advance.

Last edited by pludi; 01-11-2010 at 06:29 PM.. Reason: improved readability
# 2  
Old 01-11-2010
Debian

Quote:
awk ' { rnumber | getline num; sub("segundos",num); close(rnumber) } 1 ' rnumber='echo $((RANDOM%49+10))' inserts_$2.sql
# 3  
Old 01-11-2010
Looks it can be used to buy my lottery.
Code:
$ cat urfile
segundos
segundos
segundos
segundos
segundos
segundos

$ awk ' { rnumber | getline num; sub("segundos",num); close(rnumber) } 1 ' rnumber='echo $((RANDOM%49+10))' urfile
58
26
42
32
15
40

# 4  
Old 01-12-2010
Excelent solution. Thank you!
# 5  
Old 01-13-2010
Code:
{print "INSERT INTO $2 (DATETIME, NAME, CODE) values(TO_DATE( '"\$1 ":segundos', 'DD/MM/YYYY HH24:MI:SS'), "\$2 ", "\$3 ");"}

Replace with

Code:
{print "INSERT INTO $2 (DATETIME, NAME, CODE) values(TO_DATE( '"\$1 ":`date +"%S"`', 'DD/MM/YYYY HH24:MI:SS'), "\$2 ", "\$3 ");"}


Then get rid of the sed command altogether.
# 6  
Old 01-13-2010
Quote:
Originally Posted by jgt
Code:
{print "INSERT INTO $2 (DATETIME, NAME, CODE) values(TO_DATE( '"\$1 ":segundos', 'DD/MM/YYYY HH24:MI:SS'), "\$2 ", "\$3 ");"}

Replace with

Code:
{print "INSERT INTO $2 (DATETIME, NAME, CODE) values(TO_DATE( '"\$1 ":`date +"%S"`', 'DD/MM/YYYY HH24:MI:SS'), "\$2 ", "\$3 ");"}


Then get rid of the sed command altogether.
This also gives me the same number for all the lines! I need a different number for each line.
# 7  
Old 01-13-2010
For some reason I keep getting the same number. However, this worked in bash:
Code:
awk '{getline n<rnr; sub("segundos",n)}1' rnr=<(while : ; do echo $((RANDOM%49+10));done) inserts_$2.sql

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed random \n for "n" range of character occurrences

I'd like to put paragraph breaks \n\n randomly between 5 - 10 occurrences of the dot character (.), for an entire text file. How to do that? In other words, anywhere between every 5 -10 sentences, a new paragraph will generate. There are no other uses of the (.) except for sentence breaks in... (11 Replies)
Discussion started by: p1ne
11 Replies

2. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

3. Shell Programming and Scripting

Gen random char then sed replacements

Hey guys, I need to first generate some random characters, which I am already doing perfectly as follows: randomize=`cat /dev/urandom | tr -dc "a-z0-9" | fold -w 6 | head -n 1` This is where I am stuck...I need to sed replace some static values with those random characters, but I need each... (4 Replies)
Discussion started by: holyearth
4 Replies

4. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

5. Shell Programming and Scripting

Using SED to replace a random number?

Hi all, I need to run a number of scripts which have a certain phrase on them so I have identified these by; grep -l 100 *script | sort -u Normally I could just run something along the lines of; for i in `grep -l 100 *script | sort -u`; do ./${i}; done However before I run each of... (0 Replies)
Discussion started by: JayC89
0 Replies

6. UNIX for Dummies Questions & Answers

Sed to replace second number in a random string

I need a sed line that will take STDM111 and change it to STDM161 the STDM will always be constant but the 3 numbers after will be random, I just need it to always replace the middle number with 6 regardless of what the numbers are. (8 Replies)
Discussion started by: glev2005
8 Replies

7. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

8. Shell Programming and Scripting

help with ksh/awk/sed script, random # of fields

Hello all, I'm working on an attendance callout script for a school district. I need to change our current layout for the vendor. Currently the data is in the form of: studentid,period,building, Heres a sample of some made up records: 500,1,30, 500,2,30, 500,3,30, 500,6,30,... (7 Replies)
Discussion started by: axo959
7 Replies

9. Shell Programming and Scripting

$random

I need to use the $RANDOM command to get a line from a list of lines in a file randomly. file is help go three house film how do i randomly get one word without looking into the file? (6 Replies)
Discussion started by: relle
6 Replies

10. UNIX for Dummies Questions & Answers

Sed - Replacing numbers w/asteriks in random location in a file

I have a file which contains lots of text (comment field). I would like to parse through the comment field which can be up to 255 characters long and look for anything that seems to resemble, say, a credit card number or customer account number, etc. and replace the numbers with asteriks (*). ... (9 Replies)
Discussion started by: giannicello
9 Replies
Login or Register to Ask a Question