Why is SED so slow?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why is SED so slow?
# 1  
Old 02-04-2013
Why is SED so slow?

I have many files which contain about two million lines.
Now I want to use sed to delete the 9th line and add a new
line behind the 8th line. I use the command as follows:
Code:
for((i=1;i<100;i++));
do
  echo $i;
  sed -i '9d' $i.dat;
  sed -i '8a this is a new line' $i.dat;
done

But it is very slow for every file.
Does anybody have a better way to do that?
thank you.

Last edited by Scott; 02-04-2013 at 02:21 PM.. Reason: Please use code tags
# 2  
Old 02-04-2013
You are reading, overwriting, and deleting the entire file each loop. ...Twice.

Unfortunately if the file is huge, this may never be fast, because you cannot really avoid having to write all the old data. Files do not have an 'insert' or 'delete' operation for data in the middle of them, all the data after the change must be rewritten in the new spot.

This avoids needing to alter the file twice by just printing an alternate line for line 9 instead of deleting then inserting.

Code:
awk 'NR==9 { print "replacement line" ; next } 1' inputfile > outputfile

# 3  
Old 02-04-2013
You might even be able to do it all in one command:

Code:
awk 'FNR==9 { print "Replacement line" > FILENAME".new" ; next } { print > FILENAME".new" }
        (F != FILENAME) { if(F) close(F".new"); F=FILENAME }' *.dat

for FILE in *.new
do
        echo mv "${FILE}" ${FILE%.new}"
done

Remove 'echo' once you've tested and are sure it does what you want.

Use nawk on solaris.

Last edited by Corona688; 02-04-2013 at 03:02 PM..
# 4  
Old 02-04-2013
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Very Slow

Hi We are using sed to clean up a file of a pattern and its talking a lot of time on XML output file The command that we are using is sed -e "s/tns1://g" $OUTPUTFILENM > $TEMPFILE Where $OUTPUTFILENM is the file to be cleaned and $TEMPFILE is the cleaned output Can you... (3 Replies)
Discussion started by: jimmyb
3 Replies

2. UNIX for Advanced & Expert Users

sed working slow on big files

HI Experts , I'm using the following code to remove spaces appearing at the end of the file. sed "s/*$//g" <filename> > <new_filename> mv <new_filename> <filename> this is working fine for volumes upto 20-25 GB. for the bigger files it is taking more time that it is required... (5 Replies)
Discussion started by: sumoka
5 Replies

3. Shell Programming and Scripting

cut, sed, awk too slow to retrieve line - other options?

Hi, I have a script that, basically, has two input files of this type: file1 key1=value1_1_1 key2=value1_2_1 key4=value1_4_1 ... file2 key2=value2_2_1 key2=value2_2_2 key3=value2_3_1 key4=value2_4_1 ... My files are 10k lines big each (approx). The keys are strings that don't... (7 Replies)
Discussion started by: fzd
7 Replies

4. Shell Programming and Scripting

Cut too slow

Hi I am using a cut command in the script which is slowing down the performance of the script .can anyone suggest the other ways of doing the same cut command Record_Type=`echo "$line" | cut -c19-20` *******this is slowing down********* i have 4 more cut commands 2 in one loop and 2 in inner... (3 Replies)
Discussion started by: pukars4u
3 Replies

5. Solaris

Slow Login

Hi All, I have problem when i write my user name to login to my server late (about 10 min) to give me field of password if u know how i can solve it? Thanks (4 Replies)
Discussion started by: mass1123
4 Replies

6. UNIX for Dummies Questions & Answers

Slow System

Hi, I have an SCO-Unix server running. There are some processes (unknown to me) which consume a lot of the system resources. This slows down the server dramatically. Is there a command or program that monitors what processes are using the cpu, disk, etc.. and tell me how excessive and how... (3 Replies)
Discussion started by: Hansaplast
3 Replies

7. Post Here to Contact Site Administrators and Moderators

Slow

The site has gone slow for quite some time... Can you do somethin abt it (2 Replies)
Discussion started by: DPAI
2 Replies
Login or Register to Ask a Question