The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Compare 2 sorted files varungupta Shell Programming and Scripting 7 01-25-2008 09:07 AM
sorted processes pro Shell Programming and Scripting 7 05-19-2007 12:52 AM
Need to display sorted output on 1 line! knc9233 UNIX for Dummies Questions & Answers 2 01-17-2007 06:26 PM
Trying to get list of logged on users sorted kungfuice Shell Programming and Scripting 7 07-26-2006 11:35 AM
comm - sorted result issues krsunderm UNIX for Dummies Questions & Answers 1 11-29-2005 03:36 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-23-2007
Registered User
 

Join Date: Mar 2006
Posts: 11
Inserting in sorted rows

Hello Friends,

I have a file with many many rows and I want to insert some numbers in between numbers.
The number must be inserted in "correct sorted order".

1. Input = 1055555

0000001
1000000
1055555 <-insert here
3000000
4000000
5000000
8000000

2. Input = 4000080
0000001
1000000
3000000
4000000
4000080 <-insert here
5000000
8000000

Do you have other solutions aside from this?

cat 1055555 >> Onerow.txt
sort Onerow.txt

Thank you my friend.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-24-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,432
Code:
awk '!f&&$1>n{$0=n"\n"$0;f=1}1' n="$input" filename
Use nawk or /usr/xpg4/bin/awk on Solaris.

So, you should:
Code:
awk '!f&&$1>n{$0=n"\n"$0;f=1}1' n="$input" filename>new\
&&cp filename filename.orig\
&&mv new filename
For input greater than the max value in the file:
Code:
awk '!f&&$1>n{$0=n"\n"$0;f=1}1;END{if(!f)print n}' n="$input" filename

Last edited by radoulov; 10-24-2007 at 02:37 AM.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0