Problem in sorting a text file


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Problem in sorting a text file
# 1  
Old 05-27-2012
Problem in sorting a text file

Hi;
I have a text file like this:
Code:
1
10
11
2
3
4
M
X
Y

When I sort it numerically using sort -n, it looks like this:
Code:
Y
X
M
1
2
3
4
10
11

However, I want it to be like this:
Code:
1
2
3
4
10
11
M
X
Y

Well, this was only part of text file. I have a very big text file that I want it to be sorted based on the 3rd column. Above is an example of my third column.
Please help me!

Last edited by Scott; 10-08-2012 at 02:18 PM.. Reason: Code tags
# 2  
Old 05-28-2012
Hi.

For complex ordering tasks I often use the nonstandard utility msort:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate hybrid ordering of fields, msort.
# See: http://freshmeat.net/projects/msort

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results of msort, hybrid ordering:"
msort -q --line --position 1,1 --comparison-type hybrid -- data1

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
b2
a1
b1
A1
y
m
x
1
10
11
2
3
4
M
X
Y

-----
 Results of msort, hybrid ordering:
1
2
3
4
10
11
A1
M
X
Y
a1
b1
b2
m
x
y

The msort utility was in the Debian repository for my system. See the URL noted in the script for other options.

Best wishes ... cheers, drl
# 3  
Old 05-28-2012
Code:
 
sort -n input.txt | nawk '!/[0-9]/{a=a""ORS$0}/[0-9]/{print}END{print a}'

# 4  
Old 05-28-2012
Hi.

@itkamaraj -- good solution using standard tools.

However, it looks like an empty line is being created. Was that intentional to separate the two groups?
Code:
#!/usr/bin/env bash

# @(#) user1	Demonstrate ordering of fields, sort, arrange with awk.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sort awk

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results of sort, re-order with awk:"
sort -n $FILE |
awk '
!/[0-9]/	{a=a""ORS$0}
/[0-9]/	{print}
END	{print a}
'

exit 0

producing:
Code:
% ./user1 

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
sort (GNU coreutils) 6.10
awk GNU Awk 3.1.5

-----
 Input data file data1:
b2
a1
b1
A1
y
m
x
1
10
11
2
3
4
M
X
Y

-----
 Results of sort, re-order with awk:
A1
a1
b1
b2
1
2
3
4
10
11

M
X
Y
m
x
y

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting a text file with respect to Function/Keyword

Hello Experts, I am truly a beginner in shell and perl . Need an urgent help with sorting a file. please help. wouldn't mind whether in perl or shell script. Here are the details. ------------------------------------------------------ Input Text file EX:... (9 Replies)
Discussion started by: pradyumnajpn10
9 Replies

2. Shell Programming and Scripting

Sorting indented text files

Hello, I'm trying to find a solution or a proper tool for the following job: I need to sort a text document with indented sections, so all levels of indentation are sorted independently for each section. Particularly, I need this for Cisco routers' running config files to compare them with... (2 Replies)
Discussion started by: kobel
2 Replies

3. Shell Programming and Scripting

sorting based on a specified column in a text file

I have a tab delimited file with 5 columns 79 A B 20.2340 6.1488 8.5086 1.3838 87 A B 0.1310 0.0382 0.0054 0.1413 88 A B 46.1651 99.0000 21.8107 0.2203 89 A B 0.1400 0.1132 0.0151 0.1334 114 A B 0.1088 0.0522 0.0057 0.1083 115 A B... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

4. Shell Programming and Scripting

Chemistry problem- File matching and Sorting!!!

Dear Programmers I have a file called ranking.txt, in which I have 4 chemical compounds in *.sdf file format named ligands_m1, ligands_m2, ligands_m3, ligands_m4. Each compounds is assigned with a particular score along with the file location. ... (0 Replies)
Discussion started by: robertselwyne
0 Replies

5. UNIX for Dummies Questions & Answers

Sorting file with columns (problem)

Hi all! I am new to Unix programming so bare with me please :). I have saved the output of my results in a file called testfile which contains 3 columns a 15 rows. e.g. 175 754 abvd 948 454 fewf 43 754 fewc 6 734 feww xxx xxx xxxx I want to sort the contents of this file... (10 Replies)
Discussion started by: daelas
10 Replies

6. Shell Programming and Scripting

Sorting a text file

In unix how to sort in reverse order based on second field in a text file. $ cat data1 David:501 Albie:503 Shaun:502 The expected output: Albie:503 Shaun:502 David:501 Please help :) (4 Replies)
Discussion started by: jon2ryhme
4 Replies

7. UNIX for Dummies Questions & Answers

sorting files with find command before sending to text file

i need help with my script.... i am suppose to grab files within a certain date range now i have done that already using the touch and find command (found them in other threads) touch -d "$date_start" ./tmp1 touch -d "$date_end" ./tmp2 find "$data_location" -maxdepth 1 -newer ./tmp1 !... (6 Replies)
Discussion started by: deking
6 Replies

8. Shell Programming and Scripting

awk error in sorting text file

Hi Having a file as below file.txt error Server Network Name Dept Date Time =========================================================================================================================== 0 ServerA LAN1 AAA IT01 04/30/2008 09:16:26 0 ... (3 Replies)
Discussion started by: karthikn7974
3 Replies

9. Shell Programming and Scripting

Sorting rules on a text section

Hi all My text file looks like this: start doc ... (certain number of records) REC3|Emma|info| REC3|Lukas|info| REC3|Arthur|info| ... (certain number of records) end doc start doc ... (certain number of records)... (4 Replies)
Discussion started by: Indalecio
4 Replies

10. Shell Programming and Scripting

Problem in sorting the file

file format is word filename no.of occurances ------------------------------ wish f3.txt 2 wish f2.txt 1 cold f1.txt 5 cold f2.txt 3 cold f1.txt 3 cold e.txt 3 gold f1.txt 7 gold f3.txt 3 rush e.txt 2 itz abt building a search index for every word in the files given as input the... (1 Reply)
Discussion started by: vishnu_vaka
1 Replies
Login or Register to Ask a Question