Require help to sort string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Require help to sort string
# 1  
Old 11-10-2009
Require help to sort string

Hi Folks,

Currently am working with 10g db. I want to sort the below o/p.
Current output:
============
SQL> select partition_name from user_tab_partitions where table_name='USER_AUDIT';

PARTITION_NAME
------------------------------
PARTMAX
PART_AUDIT_NOV02
PART_AUDIT_NOV08
PART_AUDIT_NOV14
PART_AUDIT_OCT27

Expected output:
=============
PART_AUDIT_OCT27
PART_AUDIT_NOV02
PART_AUDIT_NOV08
PART_AUDIT_NOV14
PARTMAX

becasue am writing a script to drop a oldest partition automatically through a shell script. Can anybody help me?
# 2  
Old 11-10-2009
If this is to be a regular process it is very complex because of the alphabetic month abbreviations and the absence of the year in the data.
Surely this would be better as a full blown SQL program which could deduce the year, translate the data into a sortable format, sort to order and run the required SQL.
# 3  
Old 11-11-2009
Quote:
Originally Posted by methyl
If this is to be a regular process it is very complex because of the alphabetic month abbreviations and the absence of the year in the data.
Surely this would be better as a full blown SQL program which could deduce the year, translate the data into a sortable format, sort to order and run the required SQL.

Hi methyl,

or I can select another one column also with that partition name as below.

SQL> select partition_name,high_value from user_tab_partitions where table_name='USER_AUDIT';
PARTITION_NAME HIGH_VALUE
------------------------------ --------------------------------------------------------------------------------
PARTMAX MAXVALUE
SAS_AUDIT_NOV02 TO_DATE(' 2009-11-02 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
SAS_AUDIT_NOV08 TO_DATE(' 2009-11-08 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
SAS_AUDIT_NOV14 TO_DATE(' 2009-11-14 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
SAS_AUDIT_OCT27 TO_DATE(' 2009-10-27 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA

is there any way to sort with the help of high value column?

---------- Post updated 11-11-09 at 02:21 AM ---------- Previous update was 11-10-09 at 01:26 PM ----------

hi Folks,

Can anybody help me to sort the below output?

PARTITION_NAME HIGH_VALUE
------------------------------ --------------------------------------------------------------------------------
SAS_AUDIT_NOV02 TO_DATE(' 2009-11-02 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
SAS_AUDIT_NOV08 TO_DATE(' 2009-11-08 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
SAS_AUDIT_NOV14 TO_DATE(' 2009-11-14 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
SAS_AUDIT_OCT27 TO_DATE(' 2009-10-27 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA

This output will be stored in a file. From that I can print the date only as below by using cat output.lst|awk '{print $3}'|sort -n

2009-10-27
2009-11-02
2009-11-08
2009-11-14

Now I need to take the partition_ name of oldest date. In this scenario, the oldest date is 2009-10-27 and corresponding partition_name is SAS_AUDIT_OCT27. So I need the output as below.

Required output:
============
SAS_AUDIT_OCT27
# 4  
Old 11-11-2009
Maybe try keeping the date with the partition name during the sort?

Code:
cat output.lst|awk '{print $3,$1}' | sort -n | head -1 | awk '{print $2}'


Last edited by methyl; 11-11-2009 at 09:04 AM.. Reason: head
# 5  
Old 11-12-2009
Thanks methyl. I selected the partition name through the way as below.

partition_name=sed '/^$/d' partition_name.lst|awk '{print $3,$1}'|sort -n|head -1|awk '{print $2}'
echo $partition_name

The output I got SAS_AUDIT_OCT27

Now I need to drop this partition.

to connect oracle and execute a query, am using the following way.

export ORACLE_SID=test
export ORACLE_HOME=/oracle/product/10.1.0.5
export PATH=$PATH:/oracle/product/10.1.0.5/bin
sqlplus -s username/pwd << EOF
alter table table_name drop partition partition_name;
exit;
EOF

now how can I bring the partition name(partition name stored in $partition_name) into oracle?

Pls help
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 only numbers within a string

Hi, I am having contents in a file like below, cat testfile rpool/swap rpool/swap14 rpool/swap2 rpool/swap3 I want to sort the above contents like, rpool/swap rpool/swap2 rpool/swap3 rpool/swap14 I have tried in this way, (7 Replies)
Discussion started by: Sumanthsv
7 Replies

3. Shell Programming and Scripting

Sort String using awk

Hi, I need help to sort string using awk. I don't want to use sed or perl as I want to add this functionality in my existing awk script Basically I have a variable in AWK which is string with comma separated value. I want to sort that string before using that variable in further processing for... (10 Replies)
Discussion started by: rocky.community
10 Replies

4. Shell Programming and Scripting

Sort a hash based on the string length of the values

Hi, I want to be able to sort/print a hash based on the string length of the values. For example %hash = ( key1 => 'jeri', key2 => 'corona', key3 => 'una, ); I want to be able to print in the following order (smallest to largest) una,jeri,corona OR... (1 Reply)
Discussion started by: jdilts
1 Replies

5. Shell Programming and Scripting

how to sort lines in the string

Hi guys, I am trying to sort numbers in the string in descending order but for some reason sort fails. n129$ echo "81 240" | sort -r 81 240 n129$ I am not sure what am I doing wrong. Is there a 100% reliable way to make sure that sort will always work. I mean on SUNS and IBM machines. ... (4 Replies)
Discussion started by: aoussenko
4 Replies

6. Shell Programming and Scripting

Remove duplicate chars and sort string [SED]

Hi, INPUT: DCBADD OUTPUT: ABCD The SED script should alphabetically sort the chars in the string and remove the duplicate chars. (5 Replies)
Discussion started by: jds93
5 Replies

7. UNIX for Dummies Questions & Answers

List directories with given string, sort by creation date

It is for HP-Unix B.11.31. Requirement: 1. List the directories, which directories name has given particular string. Example: Directories with name "Build" 2. On the output of 1. list the directories by creation date as sort order. I tried with; find . -type d -name "Build*" ... (3 Replies)
Discussion started by: Siva SQL
3 Replies

8. Shell Programming and Scripting

Sort based on string lenght.

I'm not familiar with find. If i use find in a certain directory i want it to show based on hierarchy. find . type d fol1 fol1/subfol1 fol1/subfol1/subfol1 fol2 fol2/subfol2 i want it to show like this fol1/subfol1/subfol1 fol1/subfol1 fol1 fol2/subfol2 fol2 do i need to use... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

9. Shell Programming and Scripting

sort entire line based on part of the string

hey gurus, my-build1-abc my-build10-abc my-build2-abc my-build22-abc my-build3-abc basically i want to numerically sort the entire lines based on the build number. I dont zero pad the numbers because thats "how it is" ;-) sort -n won't work because it starts from the beginning. ... (10 Replies)
Discussion started by: gurpal2000
10 Replies

10. Shell Programming and Scripting

How to sort a string with numbers

Hi, I need help to sort a file contents. I am using sort -r option to basically reverse the comparison in descending order. However, i found out that my file is not sorted according, can anyone please help. My data is something like:- Hello world 20.982342864 343 19.234355545 222... (5 Replies)
Discussion started by: ahjiefreak
5 Replies
Login or Register to Ask a Question