sorting files in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sorting files in unix
# 1  
Old 04-25-2012
sorting files in unix

Hi ,

I have following error files in /srv/trillium/Projects/SAS_AML/logs dir.

Code:
srtforpm_p3.err
tranfrmr_p1.err
tranfrmr_p9.err
winkey_p5.err
tranfrmr_p10.err
srtforrl_p15.err
tranfrmr_p6.err
srtforrl_p7.err
tranfrmr_p8.err
tranfrmr_p14.err
tranfrmr_p13.err
tranfrmr_p12.err
rellinkref_p11.err

I need these files to be sorted in below order :
Code:
tranfrmr_p1.err
srtforpm_p3.err
winkey_p5.err
tranfrmr_p6.err
srtforrl_p7.err
'
'
'
'
'
'
'
'
'
'
tranfrmr_p13.err
tranfrmr_p14.err
srtforrl_p15.err

I want the files to sorted in 'p*.err' in ascending order
after ascending i need to store the contents of files in a single file as Trill_Error.log

Please advise

Regards,
Sonu
# 2  
Old 04-25-2012
one way:
Code:
 ls *.err | awk -F '[\._]' '{print substr($2,2), $0}'  | sort -n | awk '{printf("%s ", $2}' > listfile
cat $( cat listfile) > Trill_Error.log

# 3  
Old 04-25-2012
Code:
# ls -1 *.err|awk -F'[_.]' '{sub("p","",$2);printf "%s%s%02d\n",$1," ",$2}'|sort -k2,2|awk '{sub(/^/,"p",$2);sub(/p0/,"p",$2);print $1"_"$2".err"}'|\
while read sorted ; do cat $sorted >>Trill_Error.log ; done

# 4  
Old 04-25-2012
Another way. Temporarily change the _p string to _p. so that the numeric portion of the filename is delimited by . characters when we want to sort.

Code:
ls -1d *\.err|sed -e 's/_p/_p./g' |  sort -t '.' -k2n | \
              sed -e 's/_p./_p/g' > Trill_Error.log

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Complex data sorting in excel files or text files

Dear all, I have a complex data file shown below,,,,, A_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 C_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 A_ABCDEF715 52410.9 18598.2 10611 10754.7 122535 252426 36631.4 C_DBCDI_1353 0... (19 Replies)
Discussion started by: AAWT
19 Replies

2. Shell Programming and Scripting

sorting in UNIX

hi experts, I have a flat file with 2 fields, 1st field is alpha-numeric and 2nd is numeric. Input file is :: A_0 11 A_0 12 A_0 13 C_0 3 B_1 21 B_1 22 A_0 1 A_0 2 I want to sort this file, first based on 1st field, then on 2nd field Output should be :: A_0 1 A_0 2 A_0 11... (1 Reply)
Discussion started by: sandeepkmehra
1 Replies

3. UNIX for Dummies Questions & Answers

Sorting binary files using UNIX sort

Hi, Can i sort binary files using unix sort ? (4 Replies)
Discussion started by: AmbikaValagonda
4 Replies

4. Shell Programming and Scripting

string sorting in unix

Hi I need how to sort string characters for Example i have a file that contains this data example string "fan" but i want to display "afn" contained words "afn" is in sorted format for fan. File data faty gafny gaifny dafan gafnniunt O/p gafny gafnniunt (3 Replies)
Discussion started by: polineni
3 Replies

5. UNIX for Advanced & Expert Users

Need help for sorting in Unix

Hi I have a query regarding syncsort in Unix. What is the difference between Syncsort and normal sort. If I have 4 columns to do sort in a csv file and and first col, and third col to be done in descending order. How can I do that in Unix/ Please help me..Its urgent. (3 Replies)
Discussion started by: srinu19
3 Replies

6. UNIX for Dummies Questions & Answers

Sorting in Unix

Hi I'm writing a shell that goes through a bunch of files and does a simple test on each. Each file has a numeric name (ex. 100.jpg). My problem is that the shell is going through the files in alphabetical rather than numeric order. Thus, after checking file 19.jpg it skips to 100.jpg. Once... (7 Replies)
Discussion started by: Bengel
7 Replies

7. Shell Programming and Scripting

unix sorting

Hi experts, i have a file.If i will delete some intermediate records from the file then the output file will be in sorted format as show below. file A ==== D001 ty gh D002 fg hi D003 jk lr . . . if i will delete the 2nd record then the output file is as follows: outputfile:... (1 Reply)
Discussion started by: subhendu81
1 Replies

8. UNIX Desktop Questions & Answers

need unix script for sorting

Hi All, I am new to unix,please help me on the following its urgent I have 2 question 1 question I need a script for following senario I have get some files in directory by using grep i need to sort all files to new files for example (abc.dat --> abc.dat.sort) ex:grep *050508* ... (3 Replies)
Discussion started by: cgreddy2020
3 Replies

9. Shell Programming and Scripting

ascii sorting in unix

Hi all i am facing a problem in sorting command. The script depending on the sorting command works fine only if ascii sorting is done. i need to know how to find out how to perform ascii sorting. sorting is case insensitive in my file has data in the following format. AA/BB/ AAA/BB\ also... (1 Reply)
Discussion started by: sais
1 Replies

10. Shell Programming and Scripting

Sorting Program In UNIX

Write a shell program that takes one or any number of file names as input; sorts the lines of each file in ascending order and displays the non blank lines of each sorted file and merge them as one combined sorted file. The program should generate an error message in case any input file does not... (1 Reply)
Discussion started by: kumar_saurabh
1 Replies
Login or Register to Ask a Question