Insert a line in a sorted text file(s)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Insert a line in a sorted text file(s)
# 1  
Old 07-15-2011
Insert a line in a sorted text file(s)

Hello,

I am trying to add a line (usually just a word) to some text files in a directory that are already sorted. I just don't want to run the sort command again because it can take a long time when the text or log files are really huge. I have a bashscript that will take in the 1st argument ($1) and add it to the .txt files by appending the word to the end, then sorts it and the output goes to a temp file, it takes forever because some .txt files are very large, (over 1 gig). Any suggestions to add a word quicker? This is what I have:



Code:
#!/bin/bash 

for iFile in `ls *.txt`
do
echo $iFile

echo $1 >> $iFile | cat $iFile | sort > temp.txt
  rm $iFile
  mv temp.txt $iFile


done

Thanks

Last edited by raptor25; 07-15-2011 at 05:34 PM..
# 2  
Old 07-15-2011
Code:
nawk -v new="$1" '!done && $0 > new { print new RS $0; done=1; next } 1' sorted

# 3  
Old 07-15-2011
Thanks for the reply, but that doesn't seem to be working. It could be I am doing it wrong. Could you explain what that nawk command is doing?
# 4  
Old 07-15-2011
Try this version:
Code:
#!/bin/sh

for iFile in *.txt; do
  echo $iFile
  { rm "$iFile"; { cat -; echo $1;} | sort > "$iFile";} < "$iFile" 
done

exit 0

# 5  
Old 07-15-2011
Like this?
Code:
for iFile in `ls *.txt`
do
  echo $iFile
  nawk -v new="$1" '!done && $0 > new { print new RS $0; done=1; next } 1' $iFile >temp.txt
  rm $iFile
  mv temp.txt $iFile
done

It goes through each line until it finds the line which is lexicographically greater, then it prints $1 before it. Then it stops messing with the data and just prints the rest...
This User Gave Thanks to neutronscott For This Post:
# 6  
Old 07-15-2011
Hi.

Two thoughts:

1) If you have only one line to add, then it is, in effect, an already sorted file, so you can use the merge option in sort with your already-sorted large file. That's effectively a high-speed copy.

2) (Old-timers will remember this). You could patch in-place by replacing exactly one line in the file with a pointer to an auxiliary file that holds the line you have extracted from the main file, and the new line. You would need a special reading routine that recognizes the pointer, reads the auxiliary file, and comes back to read the main file. It would be fast, but tricky, because you have to re-write in-place. We essentially did that when we programmed in absolute octal and we needed to patch code.

Good luck ... cheers, drl
This User Gave Thanks to drl For This Post:
# 7  
Old 07-15-2011
Quote:
Originally Posted by tukuyomi
Try this version:
Code:
#!/bin/sh

for iFile in *.txt; do
  echo $iFile
  { rm "$iFile"; { cat -; echo $1;} | sort > "$iFile";} < "$iFile" 
done

exit 0

Do not use that approach unless it's acceptable to lose an entire file (either because the data is not of any importance or you have backups). If the pipeline is killed for some reason, you will lose data.

Personally, I would go with sort -m. As drl mentioned, this situation is dealing with two sorted files (the large pre-sorted file and a word which could easily be written to a temp file).

Regards,
Alister

---------- Post updated at 05:48 PM ---------- Previous update was at 05:46 PM ----------

Quote:
Originally Posted by raptor25
Code:
for iFile in `ls *.txt`

Nothing good can come from using ls and command substitution in that manner. Only problems. Just use a simpler, safer, more efficient file glob.

Code:
for iFile in *.txt

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. UNIX for Dummies Questions & Answers

Insert a line in a text file

I want to insert a line with text after the 9th line of a text file. How would I do this using sed or awk? (2 Replies)
Discussion started by: lost.identity
2 Replies

4. Shell Programming and Scripting

Sort a line and Insert sorted word(s) in a line

Hello, I am looking to automate a task - which is updating an existing access control instruction of a server and making sure that the attributes defined in the instruction is in sorted order. The instructions will be of a specific syntax. For example lets assume below listed is one of an... (6 Replies)
Discussion started by: sanjayroc
6 Replies

5. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

6. Shell Programming and Scripting

Sed insert text at first line of empty file

I can't seem to get sed to allow me to insert text in the first line of an empty file. I have a file.txt that is a 0 byte file. I want sed to insert " fooBar" onto the first line. I've tried a few options and nothing seems to work. They work just fine if there's text in the file tho. Help? (4 Replies)
Discussion started by: DC Slick
4 Replies

7. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

8. Shell Programming and Scripting

Insert a line in a text file

So I need to write lines into line X of file X. I can get the file by doing: cfgnumber=$(cat -n -comm.cfg| grep -i "servicegroup_name 24x7-comunicacions") echo $cfgnumber it outputs the Line where it finds now I need to start writing something right bellow that line. thanks (10 Replies)
Discussion started by: 4scriptmoni
10 Replies

9. Shell Programming and Scripting

how to insert a extra line in a text file

how to insert a extra line in a text file using a sh command iam trying to think of a way to add a extra line but without deleting the whole text do anyone have any ideas (2 Replies)
Discussion started by: bhaviknp
2 Replies

10. Shell Programming and Scripting

Insert text file at a certain line.

I need to insert a file called temp_impact (which has about 15 lines in it) to a file called 11.23cfg starting at line 33. I searched the forums and found the sed '34i\ test' 11.23cfg > newfile That will enter word test at the appropriate line, but i need the entire file dumped there. Any... (4 Replies)
Discussion started by: insania
4 Replies
Login or Register to Ask a Question