Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort command

Status
Not open for further replies.

ksbrace

Programmer
May 13, 2000
501
US
Hello, I am looking to use the 'sort' command...I think. I have a file where I want to add a line and then I want to sort a section of the file. For example: I want to add this line:
<A HREF="LYNXEXEC:clear;sudo su - ddd_dev">bro_dev</A><BR>
and then add it between the ccc_dev user and the eee_dev user. I have seen a lot of examples using sort, but I haven't found any that only sort part of the file. So, any guidance or example sites would be great! Thanks in advance.

Code:
<HTML>
<BODY>
<B>Development Campuses for support</B><BR>
<A HREF="LYNXEXEC:clear;sudo su - aaa_dev">bro_dev</A><BR>
<A HREF="LYNXEXEC:clear;sudo su - bbb_dev">cli_dev</A><BR>
<A HREF="LYNXEXEC:clear;sudo su - ccc_dev">esc_dev</A><BR>
<A HREF="LYNXEXEC:clear;sudo su - eee_dev">hvc_dev</A><BR>
<A HREF="LYNXEXEC:clear;sudo su - fff_dev">ins_dev</A><BR>
<A HREF="LYNXEXEC:clear;sudo su - ggg_dev">mvc_dev</A><BR>
<A HREF="LYNXEXEC:clear;sudo su - hhh_dev">nia_dev</A><BR>
<A HREF="LYNXEXEC:clear;sudo su - iii_dev">nor_dev</A><BR>
<B>
<P><A HREF="/etc/menus/main.html">Back to Main Menu</A></P>
</BODY>
</HTML>
 

Why would you need a script to do a simple editing task? [ponder]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
You can't sort part of a file. You can, however, split the file into three parts, add you row(s) to the middle part, and cat the file parts back together into one file.

see split, head, and tail commands for splitting file
see sort for sorting
see cat for concatenation of several files into one file.

-------------------------
The trouble with doing something right the first time is that nobody appreciates how difficult it was - Steven Wright
 
Where are you getting the new bit of text?

You could chop up the file with a script, sort, and concatenate, e.g.

Code:
INPUT_FN="$1"
sed -n '/LYNX/q;p' ${INPUT_FN} > $$.x
(
  sed '/LYNX/!d' ${INPUT_FN}
  echo '<A HREF="LYNXEXEC:clear;sudo su - ddd_dev">bro_dev</A><BR>'
) | sort > $$.y
sed -n '/LYNX/,${/LYNX/d;p;}' ${INPUT_FN} > $$.z
cat $$.[xyz] > ${INPUT_FN}
rm $$.[xyz]

You could have some real fun with VI using registers to save the head and tail. Then do %!sort on the remaining /LYNX/ stuff. The bring back the head and tail - all in macros.

Cheers,
ND [smile]

[small]bigoldbulldog AT hotmail[/small]
 
Right on LKBrwnDBA! Unless the string comes from some process, just use your favorite editor.

Cheers,
ND [smile]

[small]bigoldbulldog AT hotmail[/small]
 
Ok, Thanks for the ideas....

LKBrwnDBA,
Yes, adding one line is no big deal..but when you have to do this for 200+ libraries, a script would be better...I think. I was thinking of implementing some sorting algorithm after I store the lines into an array.


 
Code:
#!/bin/bash
#
# usage: partsort FROM TO FILE
#

FROM=$1
TO=$2
FILE=$3

NEWLINE=$(cat new.line)

function catTo
{
	sed -n --expression="1,/$FROM/p" $FILE
}

function catFromTo
{
	sed -n --expression="/$FROM/,/$TO/p;/$FROM/a$NEWLINE" $FILE | sort
}

function catFrom
{
	sed --expression="1,/$TO/d" $FILE
}

catTo | sed --expression="/$FROM/d"
catFromTo
catFrom
echo
put your new line in a file new.line, and call the above script:
Code:
partsort ccc_dev eee_dev xy.html

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top