The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > Windows & DOS: Issues & Discussions
Google UNIX.COM


Windows & DOS: Issues & Discussions Questions involving Unix to Windows (Desktop or Server) go here. Any Windows/DOS questions should go here as well.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extracting data from text file based on configuration set in config file suparnbector Shell Programming and Scripting 3 08-09-2007 11:25 PM
Exporting text file data to csv l_jayakumar Shell Programming and Scripting 3 09-18-2005 08:27 PM
filter data deep.singh UNIX for Dummies Questions & Answers 3 08-14-2005 08:24 PM
How to extract data from a text file negixx Shell Programming and Scripting 1 07-19-2005 06:30 PM
getting data out from a text file skotapal Shell Programming and Scripting 7 09-14-2002 08:10 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2007
Registered User
 

Join Date: Sep 2005
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Filter data from text file

Hi All

We have got a text file, which has data dumped from 60 tables.
From these 60 tables of data we need data from 4 tables only.

I tried assigning line numbers to filter out data, but it is not working as intended.

below is the sample file

----Table1-----
3,dfs,43,df
4,sd,5,edd
56,df,6,fgdg
---Table2---
54,fdgfg,fgdfg
65,dgdfg,yuytr
76,tyuuu,ytur
---Table3----
98,rere,78
87,gdg,87
45,hgff,98
---Table4---
sdfsd,tyut,676
ffsdf,ggfd,879
ghgf,iyuty,877

Say from the file, i need to filter out data for Table2 and Table 3 only ...

Can any suggest a solution for this ..

Thanks in Advance
Sri....
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-28-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 694
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
csplit -k input_file /Table/ {99}
Reply With Quote
  #3 (permalink)  
Old 06-28-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
sed -n -e "/Table2/,/^--/{/^--/!p;/Table2/p;}" -e "/Table3/,/^--/{/^--/!p;/Table3/p;}" file
Reply With Quote
  #4 (permalink)  
Old 07-09-2007
Registered User
 

Join Date: Sep 2005
Posts: 2
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thanks a Ton .. The Seds statement is working
Reply With Quote
  #5 (permalink)  
Old 04-14-2008
Registered User
 

Join Date: Apr 2008
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
how to???

Hi,

OK. This is part of solutions what I need.
How I can filter out only those tables where is information like IP=172.18.0.1?
Reply With Quote
  #6 (permalink)  
Old 04-14-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Without an example, it's hard to guess. The example you posted so far only has 12345 and asdfghjkl, which of those is supposed to be an IP address?

Maybe it would be easier if you split the single file into multiple files first. Then it's a simple matter of grep -l IP=whatever files
Reply With Quote
  #7 (permalink)  
Old 04-14-2008
Registered User
 

Join Date: Apr 2008
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thx for your answer. Here is one example.

----Table1-----
3,dfs,43,df
4,sd,5,edd
56,df,6,fgdg
IP=172.18.0.1
---Table2---
54,fdgfg,fgdfg
65,dgdfg,yuytr
76,tyuuu,ytur
IP=172.18.0.2
---Table3----
98,rere,78
87,gdg,87
45,hgff,98
IP=172.18.0.1
---Table4---
sdfsd,tyut,676
ffsdf,ggfd,879
ghgf,iyuty,877
IP=172.18.0.1
----Table5-----
3,dfs,43,df
4,sd,5,edd
56,df,6,fgdg
IP=172.18.0.2
---Table6---
54,fdgfg,fgdfg
65,dgdfg,yuytr
76,tyuuu,ytur
IP=172.18.0.5
---Table7----
98,rere,78
87,gdg,87
45,hgff,98
IP=172.18.0.10
---Table8---
sdfsd,tyut,676
ffsdf,ggfd,879
ghgf,iyuty,877
IP=172.18.0.111

In one file there is over 100 000 tables and I want to find out those tables where IP=172.18.0.1
Reply With Quote
  #8 (permalink)  
Old 04-14-2008
era era is online now
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
awk '/^----*Table/{table=$0}
/^IP=/{ print table }' file
Do you really have a variable number of dashes before the "Table"?
Reply With Quote
  #9 (permalink)  
Old 04-14-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,331
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
awk '$0 ~ v { printf "%s\n%s\n", x, $0 }
/^--/ { x = "" } { x = x ? x RS $0 : $0 }
' v="^IP=172.18.0.1$" file
Use nawk or /usr/xpg4/bin/awk on Solaris.

Perhaps I misread the question, if you need only the table names,
you should use the code era posted.
Reply With Quote
  #10 (permalink)  
Old 04-15-2008
Registered User
 

Join Date: Apr 2008
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thank you radoulov and era.

Radoulov solution very near that what I am looking for.

This script (radoulov) print out from tables only those rows which are before IP=x.x.x.x
After IP=x.x.x.x are couple rows more and I like to print out those too.
Reply With Quote
  #11 (permalink)  
Old 04-15-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,331
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
awk 'END { if (f) print x } $0 ~ v && f++
/^--/ { if (f) print x; x = f = 0 }
{ x = x ? x RS $0 : $0 }' v="^IP=172.18.0.1$" file
Reply With Quote
  #12 (permalink)  
Old 04-15-2008
Registered User
 

Join Date: Apr 2008
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
/usr/xpg4/bin/awk: syntax error Context is:
>>> END { if (f) print x } $0 ~ v && f++/---- <<<
Reply With Quote
  #13 (permalink)  
Old 04-15-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,331
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by Hone1975 View Post
/usr/xpg4/bin/awk: syntax error Context is:
>>> END { if (f) print x } $0 ~ v && f++/---- <<<
Copy/paste my code,
there's a newline after f++ ...

(why this thread is in Windows & DOS: Issues & Discussions?)
Reply With Quote
  #14 (permalink)  
Old 04-15-2008
Registered User
 

Join Date: Apr 2008
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by radoulov View Post
Copy/paste my code,
there's a newline after f++ ...

(why this thread is in Windows & DOS: Issues & Discussions?)
Why this thread: This case was very near of my needs and I am very new forum user and I didn't remenber to check is my case in the right place.

Sorry.

What do you mean new line? If I put your code to my Solaris shell window I get error message (If I copy and paste your code).
Reply With Quote
  #15 (permalink)  
Old 04-15-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,331
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Strange,
it seems that you're running this:

Code:
awk 'END { if (f) print x } $0 ~ v && f++ /^--/ { if (f) print x; x = f = 0 }
{ x = x ? x RS $0 : $0 }' v="^IP=172.18.0.1$" file
instead of this:
Code:
awk 'END { if (f) print x } $0 ~ v && f++
/^--/ { if (f) print x; x = f = 0 }
{ x = x ? x RS $0 : $0 }' v="^IP=172.18.0.1$" file
...
Anyway,
try separating the statments explicitly:

Code:
awk 'END { if (f) print x } $0 ~ v && f++;
/^--/ { if (f) print x; x = f = 0 }
{ x = x ? x RS $0 : $0 }' v="^IP=172.18.0.1$" file
Reply With Quote
Google UNIX.COM
Reply

Tags
solaris

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script rsync ftp scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com v