1835304 Members
2480 Online
110078 Solutions
New Discussion

Scripting question.

 
SOLVED
Go to solution
Fred Myers
Advisor

Scripting question.

I am writing a KSH script, and have what I hope is an easy to do thing.

I want to add a line, but not at the end of a file, I want to add it below a section of numbers with the same number and a different variable is their a way I can do this?

ie
230 george
230 paul
230 mike
230 greg

250 texas
250 arizona
250 oklahoma

I want to add 230 fred to the end of the other 230's.

Thanks
Fred
5 REPLIES 5
Anthony deRito
Respected Contributor
Solution

Re: Scripting question.

Fred, a quick way would to be simply add the string to the end of the file then pipe the entire file through a pipe to sort -n.

cat filename | sort -n > /tmp/filename_sorted

This will sort the file nicely based on the first numeric field of each row. This way you can throw tons of data at the file and do the sort at the very end.

Tony
Craig Rants
Honored Contributor

Re: Scripting question.

Anthony hits the mark right of the bat. Congrats on the new hat, also.

One thing that sort -n will do is to sort all the data so if you want it to stay in place this may not work for you. , i.e.

#cat tempfile | sort -n
230 george
230 greg
230 mike
230 paul
250 arizona
250 oklahoma
250 texas

C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Craig Rants
Honored Contributor

Re: Scripting question.

Boy, you see why I don't teach english. You get this jist, your data will not be in the same order it was in prior to the sort -n.

"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Sridhar Bhaskarla
Honored Contributor

Re: Scripting question.

Hi Fred,

Anthony's is the smart way. But if you want to retain the spaces and other extra characters as they are, you can use the following...

You can predefine NUMBER and VARIABLES in your script so that they can be used in this function like

add_it 230 Fred

add_it()
{
NUMBER=$1 STRING=$2
LINE=`grep "^$NUMBER" list |tail -1|awk '{print $2}'`
sed -e '/'$NUMBER' '$LINE'/{x;s/^/'$NUMBER' '$STRING'/;x;G;}' list

}

You may want to put other checks in this script.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Steven Sim Kok Leong
Honored Contributor

Re: Scripting question.

Hi,

Just a small little perl script I have written and tested.

This script insert.pl takes the first column and appends the new tuple to the bottom of tuples with the same primary key, regardless of lexical or numeric order.

insert.pl:
=============================
#!/usr/bin/perl

open (FILE, "tstfile");
open (NEWFILE, "> results");

$reach=0;
$end=0;

while ()
{
if (/^$ARGV[0] /)
{
$reach=1;
}
else
{
if (($reach == 1) && ($end == 0))
{
$end=1;
print NEWFILE "$ARGV[0] $ARGV[1]\n";
}
}
print NEWFILE "$_";
}

if (($reach == 1) && ($end == 0))
{
$end=1;
print NEWFILE "$ARGV[0] $ARGV[1]\n";
}

close (NEWFILE);
close (FILE);
`mv -f results tstfile`;
=============================

Screen dump of test results:
=============================
$ cat tstfile
230 george
230 paul
230 mike
230 greg

250 texas
250 arizona
250 oklahoma
$ ./insert.pl 230 bill
$ cat tstfile
230 george
230 paul
230 mike
230 greg
230 bill

250 texas
250 arizona
250 oklahoma
$ ./insert.pl 250 gates
$ cat tstfile
230 george
230 paul
230 mike
230 greg
230 bill

250 texas
250 arizona
250 oklahoma
250 gates
=============================

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com