Sponsored Content
Full Discussion: Sed and random
Top Forums Shell Programming and Scripting Sed and random Post 302386195 by Tr0cken on Monday 11th of January 2010 04:11:55 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
NWDIAG(1)						      General Commands Manual							 NWDIAG(1)

NAME
nwdiag - generate network-diagram image file from spec-text file. SYNOPSIS
nwdiag [options] files DESCRIPTION
This manual page documents briefly the nwdiag commands. nwdiag is generate sequence-diagram image file from spec-text file. OPTIONS
These programs follow the usual GNU command line syntax, with long options starting with two dashes (`-'). A summary of options is included below. For a complete description, see the Info files. -h, --help show this help message and exit. --version show program's version number and exit. -a, --antialias Pass diagram image to anti-alias filter. -c FILE, --config=FILE read configurations from FILE. -o FILE write diagram to FILE. -f FONT, --font=FONT use FONT to draw diagram. -T TYPE Output diagram as TYPE format. SEE ALSO
The programs are documented fully by http://blockdiag.com/en/nwdiag/ AUTHOR
nwdiag was written by Takeshi Komiya <i.tkomiya@gmail.com> This manual page was written by Kouhei Maeda <mkouhei@palmtb.net>, for the Debian project (and may be used by others). June 11, 2011 NWDIAG(1)
All times are GMT -4. The time now is 05:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy