[ask]SQL command act like sort and grep


 
Thread Tools Search this Thread
Top Forums Programming [ask]SQL command act like sort and grep
# 1  
Old 10-16-2012
[ask]SQL command act like sort and grep

for example, I have a text file in random content inside, maybe something like this.
Code:
234234
54654
123134
467456
24234234
7867867
23424
568567

if I run this command
Code:
cat "filename.txt" | sort -n | grep "^467456$" -A 1 -B 1

the result is
Code:
234234
467456
568567

is it possible to do this command in sql query?
the query might be like this.
Quote:
sort table A order by id where id = 467456 and get one line after and one line before it.
# 2  
Old 10-16-2012
Have a look at this. Using union merges the queries together and sorts them. Logically this is not what you asked for though. Your example was getting a row on either side of the target value. This example gets the next highest and next lowest value relative to the target value. By definition the order of rows do not matter in a relational database table, so one would not really select a row on either side of a value in practice. Hopefully someone with better SQL skills can make this more efficient.

Code:
SQL> create table test (
  2  idx number);

Table created.

SQL> insert into test values (234234);

1 row created.

SQL> insert into test values (54654);

1 row created.

SQL> insert into test values (123134);

1 row created.

SQL> insert into test values (467456);

1 row created.

SQL> insert into test values (24234234);

1 row created.

SQL> insert into test values (7867867);

1 row created.

SQL> insert into test values (23424);

1 row created.

SQL> insert into test values (568567);

1 row created.

SQL> ed
Wrote file afiedt.buf

  1  select idx
  2  from test
  3  where idx=467456
  4  union
  5  select max(idx)
  6  from test
  7  where idx < 467456
  8  union
  9  select min(idx)
 10  from test
 11* where idx > 467456
SQL> /

       IDX
----------
    234234
    467456
    568567

SQL>

These 2 Users Gave Thanks to gary_w For This Post:
# 3  
Old 10-16-2012
SQL is not a natural place for order and numbering. You can add order with 'order by' but numbering is a bit server-specific. But there are ways. Suppose your text file is in a one column table file_name with a numeric column named idx, and all values are unique. You can select the count of rows where idx < 467456, so you know its nominal 0-based number in an ordered listing. You can now use that number to select from file_name where the count of rows less minus the first subquery is 'between -1 and 1':
Code:
select
  b.idx
 from file_name b
 where ( (
      select count(c.*) from file_name c where idx < 467456
     ) - (
      select count(a.*) from file_name a where a.idx < b.idx
   ) ) between -1 and 1

This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Wget, grep, sort, sed in 1 command/script

Hi, I need to join these statements for efficiency, and without having to make a new directory for each batch. I'm annotating commands below. wget -q -r -l1 URL ^^ can't use -O - here and pipe | to grep because of -r grep -hrio "\b\+@\+\.\{2,4\}\+\b" * > first.txt ^^ Need to grep the output... (14 Replies)
Discussion started by: p1ne
14 Replies

2. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

3. Homework & Coursework Questions

Grep and Sort

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1. Print the number of people that are in the /etc/passwd file with the name of George 2. Sort by name and... (8 Replies)
Discussion started by: Jagst3r21
8 Replies

4. Shell Programming and Scripting

grep from 3 lines and sort

Pseudo name=hdiskpower54 Symmetrix ID=000190101757 Logical device ID=0601 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ============================================================================== ---------------- Host --------------- - Stor - -- I/O Path - -- Stats --- ### HW... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

5. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

6. Shell Programming and Scripting

How to grep the where clause of a SQL?

Hi UNIX Gurus, I want to use extract the where clause of a SQL present in a file. Please suggest me how can I do it. Select * from emp where emp_id>10; cat <file_name> | grep -i "where" returns whole SQL. how can I extract only "where emp_id>10;" Thanks in advance (4 Replies)
Discussion started by: ustechie
4 Replies

7. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

8. Shell Programming and Scripting

how to grep sort userids

hello folks i have a file that have data like /test/aa/123 /test/aa/xyz /test/bb/xyz /test/bb/123 in above lines i just wants to grep "aa" and "bb". Thanks, Bash (4 Replies)
Discussion started by: learnbash
4 Replies

9. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

10. UNIX for Dummies Questions & Answers

Sort/Grep Question

Hello all, I have a test file that has the format: ..... O 3.694950 -.895050 1.480000 O 5.485050 .895050 1.480000 Ti -4.590000 4.590000 2.960000 Ti -2.295000 ... (5 Replies)
Discussion started by: aarondesk
5 Replies
Login or Register to Ask a Question