Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there any way using the korn shell to delete all records in a file except one?? In other words if I had 6 lines in a file, could I issue one command and delete all rows execpt for row 3?? Any help or suggestions would be greatly appreciated...

Hi Bill,
this is easy in sed
just sed -n '3p' fileABut it didn't realy delete the rows in fileA
just print's the 3 row.
So a redirection of the out put should be used e.g. sed -n '3p' fileA > fileB
then use mv -f fileB fileA.No RISK no fun
Frank

head -3 oldfile > newfile
tail -2 oldfile >> newfile
mv newfile oldfileAlso grep -n and wc -l might be helpful to you.

Since you asked "can it be done in the Korn shell", the answer is yes.
Best way is to write a simple script that accepts an argument.
#!/bin/ksh
exec 3< /tmp/data.file
Counter=1while read -u3 Line
do
if [ $1 = $Counter ]
then
print $Line
exit
else
(( Counter += 1 ))
fi
doneYou can add a test to make sure you are getting an argument and that it is numeric.
This solution is a pure "Korn shell" solution in that it uses no external utility to accomplish the task. If you can do simple tasks without using "cat, awk, sed, head, tail, etc...", you will become a better shell script programmer. Each of the above utilities have their uses; however, it is not to perform trivial tasks that can be done within the shell. I recently rewrote a large script that was looping about 10000 times per execution. Each time it went through the loop, it was creating a new input file for sed, executing sed and using 4 awk statments to parse some fields. This is 50,000 useless processes being spawned to do a task that could be accomplished by simply opening up the file and reading in the fields. Creating a new sed input file added another 10,000 useless steps to the loop.
Jerry

I think it is tradeoff between performance and learning scripting.
If input file is very huge then I think reading whole file is not a good idea, it is definitely a time consuming. sed will work best to get job done.

If you are calling sed every time through the loop, there is no way it is more efficient. If you are doing a global change to a file, sed is the utility of choice. Size of the file is not a factor. I have opened up and parsed through files of up to 200 megs just using a simple loop and pattern matching within the loop. I stand by my statement that if you can do it in a script, do it there. If you have to use a utility, try and do it once. I have timed loops against awk and sed. Timing varies with the loop or sed usually the fastest and awk always last. Using sed in this script where the row is passed as an argument is probably better than the loop, although on a small file such as was used in the example, I'll bet there is no difference.
My point was that entirely too many solutions are overly complex. Albert Einstein once said:
Make it as simple as possible, no simplier.
I try and use that approach in shell scripting. Learn everything you can do within the shell and use utilities only when they are truly the better option. Because people don't learn the shell, I see 60,000 useless iterations of sed and awk in a simple loop when they are not needed. Neither you or anyone else will change my mind, that is simply BAD programming.

I have a flat file which 2485288 records in it.
I need to delete the 386239th record ,could
u pls help me in writing a script which is
simple.Thanks
John

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |