- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Scripting question.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2001 01:43 PM
12-05-2001 01:43 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2001 01:53 PM
12-05-2001 01:53 PM
Solutioncat 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2001 02:15 PM
12-05-2001 02:15 PM
Re: Scripting question.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2001 02:27 PM
12-05-2001 02:27 PM
Re: Scripting question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2001 03:16 PM
12-05-2001 03:16 PM
Re: Scripting question.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2001 04:41 PM
12-05-2001 04:41 PM
Re: Scripting question.
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