How to retrieve data using awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to retrieve data using awk command
# 1  
Old 07-30-2010
How to retrieve data using awk command

I have a txt file with below data (textfile1.txt)
Code:
select col1, col2 from Schema_Name.Table_Name1
select * from Schema_Name.Table_Name2
select col1, col2, col3 from Schema_Name.Table_Name3
select col1 from Schema_Name.Table_Name4

My output should look like
Code:
Table_Name1
Table_Name2
Table_Name3
Table_Name4

I have tried below command to pull Table_Name2 & 3
Code:
cat textfile1.txt |awk -F "Schema_Name| " '{split($3,v,".");print v[2]}'

Could anyone help me out to pull all table names irrespective of where it appears in a select statement.

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 07-30-2010 at 04:25 AM..
# 2  
Old 07-30-2010
Code:
$ awk -F "." '{print $2}' file1
Table_Name1
Table_Name2
Table_Name3
Table_Name4


cheers,
Devaraj Takhellambam
# 3  
Old 07-30-2010
thanks Devaraj

Could you help me out to pull Table_Name alone without alias name
Code:
select col1, col2 from schema_Name.Table_Name1 a
select * from schema_Name.Table_Name2 b
select col1, col2, col3 from schema_Name.Table_Name3 c
select col1 from schema_Name.Table_Name4 d


Last edited by zaxxon; 07-30-2010 at 04:25 AM.. Reason: code tags
# 4  
Old 07-30-2010
try
Code:
awk -F "." '{print substr($2,0,index($2," "))}' file1

or

Code:
awk -F "." '{print $2}' file1 | awk '{print $1}'

cheers,
Devaraj Takhellambam
# 5  
Old 07-30-2010
Thanks Devaraj for your reply

Here i stuck up with one more issue, see below file contents there are alias for column names too
Code:
select col1, col2 from schema_Name.Table_Name1 a
select * from schema_Name.Table_Name2 b
select c.col1, c.col2, c.col3 from schema_Name.Table_Name3 c
select d.col1 from schema_Name.Table_Name4 d

# 6  
Old 07-30-2010
Odd but
Code:
$ awk -F "from " '{print substr($2,index($2,".")+1,index($2," ")-index($2,"."))}' file1

cheers,
Devaraj Takhellambam
This User Gave Thanks to devtakh For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

awk command to retrieve record 23 and 89 from UNIX file

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: I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file?... (6 Replies)
Discussion started by: rakeshp
6 Replies

2. UNIX for Beginners Questions & Answers

awk command to retrieve record 23 and 89 from UNIX file

Hi Everyone, I am looking for awk command to retrieve only the record number 23 and record number 89 from a unix file? Please let me know what is the awk command for this? Regards Rakesh (1 Reply)
Discussion started by: rakeshp
1 Replies

3. Shell Programming and Scripting

Compare columns of two files and retrieve data

Hi guys, I need your help. I have two files: file1 1 3 5 file2 1,XX 2,AA 3,BB 4,CC 5,DD I would like to compare the first column and where they are equal to write that output in a new file: 1,XX 3,BB (7 Replies)
Discussion started by: apenkov
7 Replies

4. Shell Programming and Scripting

Sed for select and retrieve data

I would like to recover the data from 3 text tags. These three markers are located between the tags specific location <tag1> and </tag1> knowing that they are in many places. In File.txt: <tag2>txt2</tag2> <tag3>txt3</tag3> <tag4>txt4</tag4> .... <tag1> <tag2>txt2</tag2>... (3 Replies)
Discussion started by: Amad
3 Replies

5. UNIX for Dummies Questions & Answers

Retrieve data from Remote machine

Hello Please I ask if it is possible to recover data that is stored on a remote machine that I access via ssh on a usb ? if so, how? Thank you so much (5 Replies)
Discussion started by: chercheur857
5 Replies

6. Shell Programming and Scripting

Help to retrieve data from two files matching a string

Hello Experts, I have come back to this forum after a while now, since require a better way to get my result.. My query is as below.. I have 3 files -- 1 Input file, 2 Data files .. Based on the input file, data has to be retreived matching from two files which has one common key.. For EX:... (4 Replies)
Discussion started by: shaliniyadav
4 Replies

7. UNIX for Dummies Questions & Answers

How to compare two columns and retrieve data

I am a newbie to Unix and slowly learning it. I have a large data set with 8 different columns. I want to compare two columns and retrieve data if the two columns have similar number. I have attached the example. There are two columns (S-Contig and N-Contig). I want to retrieve the data from... (7 Replies)
Discussion started by: bjorngill
7 Replies

8. Shell Programming and Scripting

More time to retrieve data from DB

Hi All, It takes around one hour to retrieve 3 lakhs data from DB. I feel this can be still more reduced, please help me in improvising the below code, to get it retrieve faster, atleast 30 to 45 minutes. sqlplus -s ${OCAU_DB_UNAME}/${OCAU_DB_UPSWD}@${OCAU_DB_NAME} > /apps/data/filedata.txt... (4 Replies)
Discussion started by: pattamuthu
4 Replies

9. UNIX for Advanced & Expert Users

Retrieve data and redirect to a file

How to write a shell script to retrieve datas from database after that this database are redirect to a excell sheet and then i got a mail that gives details about the database with the column name and data.. I m using oracle 9i... Thanks, Anup Das (2 Replies)
Discussion started by: anupdas
2 Replies

10. Shell Programming and Scripting

Retrieve data from a file

Hello guys I want to retrieve two data from a file, like this: bash-2.03$ cat numtest 123456 123457 bash-2.03$ more ./test_num #!/bin/bash num1= num2= cnt=1 while read x do num${cnt}=$x cnt=$(($cnt+1)) done <$1 echo $num1 "\n" $num2 But when i executed this script, error... (2 Replies)
Discussion started by: tpltp
2 Replies
Login or Register to Ask a Question