Inserting in sorted rows


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting in sorted rows
# 1  
Old 10-24-2007
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.
# 2  
Old 10-24-2007
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 06:37 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matrix to 3 col sorted

Hello experts, I have matrices sorted by position, there are 400k rows, 3000 columns. ID CHR POS M1 M2 M3 M4 M5 ID1 1 1 4.6 2.6 2.1 3.5 4.2 ID2 1 100 3.6 2.9 3.2 2.6 2.5 ID3 1 1000 4.1... (9 Replies)
Discussion started by: senhia83
9 Replies

2. Shell Programming and Scripting

Inserting lines from one file to a sorted list

Hello friends! I am working a Psychology/Neuro* project where I am sorting inline citations by category. The final step of the process has me a little stuck. I need to take citations from a text list and sort them in another text file. Here is a file X example... (1 Reply)
Discussion started by: danbroz
1 Replies

3. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

4. UNIX for Dummies Questions & Answers

Help with printing sorted expression

Hi All, How can I print the sorted results of the following expression in Perl ?? print "$i\t$h{$i}\n"; I tried print (sort ("$i\t$h{$i}")"\n"); and other variations of the same but failed. Can someone suggest how to solve this problem, as I'm tryin print sorted results of my script, which... (11 Replies)
Discussion started by: pawannoel
11 Replies

5. UNIX for Dummies Questions & Answers

numerically sorted filenames

How do you sort filenames: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 as: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: kenneth.mcbride
6 Replies

6. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

7. UNIX for Advanced & Expert Users

Sorted file

Hi Is there any unix shell command or utility to know if the file is sorted or not? Thanks (3 Replies)
Discussion started by: ksailesh
3 Replies

8. Shell Programming and Scripting

Compare 2 sorted files

Hi all, please give me the commands using which i can compare 2 sorted files and get the difference in third file, indiating where the difference is from either file1 or file2. as: File1 (Original file) GARRY JOHN JULIE SAM --------------- File2 DEV GARRY JOHN JOHNIEE (7 Replies)
Discussion started by: varungupta
7 Replies

9. Shell Programming and Scripting

sorted processes

Hi, I am trying to make a script that creates a list of all active (alive) processes sorted by size and then print this list on screen. Could anyone help me? Thaks a lot (7 Replies)
Discussion started by: pro
7 Replies

10. UNIX for Dummies Questions & Answers

comm - sorted result issues

In AIX 5.2, we are attempting to create a delta file by comparing the prior extract to the new extract. We are having some records appear as new when we wouldn't expect it. Problem appears to be related to the appearance of a new record with a key that is wholly contained in another records... (1 Reply)
Discussion started by: krsunderm
1 Replies
Login or Register to Ask a Question