Sort data file by case


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort data file by case
# 1  
Old 09-02-2015
Sort data file by case

Hello,
I'm trying to sort a large data file by the 3rd column so that all of the first words in the 3rd column that are in all uppercase appear before (or after) the non uppercase words. For example,

Data file:

Code:
xxx	12345		Rat in the house
xxx	12345		CAT in the hat
xxx	12345		Dog in the yard
xxx	12345		BAT in the belfry
xxx	12345		Duck in the pond
xxx	12345		ANT in the garage

Desired output:

Code:
xxx	12345		CAT in the hat
xxx	12345		BAT in the belfry
xxx	12345		ANT in the garage
xxx	12345		Rat in the house
xxx	12345		Dog in the yard
xxx	12345		Duck in the pond


In this example, the ordering of the first three lines of the output file (and the last three lines) doesn't matter, as long as they are separated by case.
Thanks very much!
# 2  
Old 09-02-2015
You could try something like:
Code:
tmpfile="NC$$"
> "$tmpfile"
awk -v f="$tmpfile" '
match($3, /[^[:upper:]]/) {
	print > f
	next
}
1' file
cat "$tmpfile"
rm -f "$tmpfile"

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.

if file contains the data in your sample input file, it writes exactly what you said you wanted to standard output.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-02-2015
Hi.
Code:
       -d, --dictionary-order
              consider only blanks and alphanumeric characters

-- man sort

From your sample input file:
Input data file data1:
Code:
xxx	12345		Rat in the house
xxx	12345		CAT in the hat
xxx	12345		Dog in the yard
xxx	12345		BAT in the belfry
xxx	12345		Duck in the pond
xxx	12345		ANT in the garage

via:
sort -d -k3,3 data1
to:
Code:
xxx	12345		ANT in the garage
xxx	12345		BAT in the belfry
xxx	12345		CAT in the hat
xxx	12345		Dog in the yard
xxx	12345		Duck in the pond
xxx	12345		Rat in the house

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 4  
Old 09-02-2015
Quote:
Originally Posted by drl
Hi.
Code:
       -d, --dictionary-order
              consider only blanks and alphanumeric characters

-- man sort

From your sample input file:
Input data file data1:
Code:
xxx	12345		Rat in the house
xxx	12345		CAT in the hat
xxx	12345		Dog in the yard
xxx	12345		BAT in the belfry
xxx	12345		Duck in the pond
xxx	12345		ANT in the garage

via:
sort -d -k3,3 data1
to:
Code:
xxx	12345		ANT in the garage
xxx	12345		BAT in the belfry
xxx	12345		CAT in the hat
xxx	12345		Dog in the yard
xxx	12345		Duck in the pond
xxx	12345		Rat in the house

Best wishes ... cheers, drl
Using sort -d works with the sample data given because ANT, BAT, and CAT (starting with A, B, and C) come before Dog, Duck, and Rat (starting with D and R). If ANT had been Ant and Dog had been DOG, sort -d would not put DOG before Ant.
# 5  
Old 09-02-2015
Hi, Don.
Quote:
Originally Posted by Don Cragun
Using sort -d works with the sample data given because ANT, BAT, and CAT (starting with A, B, and C) come before Dog, Duck, and Rat (starting with D and R). If ANT had been Ant and Dog had been DOG, sort -d would not put DOG before Ant.
Agreed ... 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

Sort file data according to a custom list of string

I have a string of pre defined ip address list which will always remain constant their order will never change like in below sample: iplist=8.8.5.19,9.7.5.14,12.9.9.23,8.8.8.14,144.1.113 In the above example i m considering only 5 ips but there could be many more. Now i have a file which... (15 Replies)
Discussion started by: mohtashims
15 Replies

2. Shell Programming and Scripting

Sort data in text file in particular format

I have to sort below output in text file in unix bash 20170308 DA,I,113 20170308 PM,I,123 20170308 DA,U,22 20170308 PM,U,123 20170309 DA,I,11 20170309 PM,I,23 20170309 DA,U,123 20170309 PM,U,233 (8 Replies)
Discussion started by: Adfire
8 Replies

3. UNIX for Advanced & Expert Users

Sort mixed data file

I have a text file and each field is separated by semicolon ( ; ). Field number 7 is internally separated by comma ( , ) and pipe ( | ) symbol. I want to sort file based on three different fields which are marked in BOLD. Here first BOLD field will have numbers upto the length of 9 characters,... (6 Replies)
Discussion started by: jnrohit2k
6 Replies

4. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

Advanced: Sort, count data in column, append file name

Hi. I am not sure the title gives an optimal description of what I want to do. Also, I tried to post this in the "UNIX for Dummies Questions & Answers", but it seems no-one was able to help out. I have several text files that contain data in many columns. All the files are organized the same... (14 Replies)
Discussion started by: JamesT
14 Replies

6. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

7. Shell Programming and Scripting

Ignore Header and Footer and Sort the data in fixed width file

Hi Experts, I want to Sort the data in fixed width file where i have Header and Footer also in file. I m using below commad to do the sort based on field satarting from 15 position to 17 position , but it is not ignoring the Header and Footer of the file while sorting. In the output i am... (5 Replies)
Discussion started by: sasikari
5 Replies

8. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

9. Shell Programming and Scripting

Sort a big data file

Hello, I have a big data file (160 MB) full of records with pipe(|) delimited those fields. I`m sorting the file on the first field. I'm trying to sort with "sort" command and it brings me 6 minutes. I have tried with some transformation methods in perl but it results "Out of memory". I was... (2 Replies)
Discussion started by: rubber08
2 Replies

10. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies
Login or Register to Ask a Question