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....

csplit -k input_file /Table/ {99}
sed -n -e "/Table2/,/^--/{/^--/!p;/Table2/p;}" -e "/Table3/,/^--/{/^--/!p;/Table3/p;}" file

Thanks a Ton .. The Seds statement is working :slight_smile:

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?

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

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

awk '/^----*Table/{table=$0}
/^IP=/{ print table }' file

Do you really have a variable number of dashes before the "Table"?

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.

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.

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

/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?)

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).

Strange,
it seems that you're running this:

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:

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:

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

How I run that awk script in Solaris unix shell? (Couple of lines, error comes if I run it on one line).

The last version should work well even on one line,
could you post the command and the errors (just copy/paste them from your terminal)?

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$" list.txt
Illegal variable name.

I cannot reproduce it, could you try it with nawk?

Try this also:

  1. Put this code into a file (for example Hone1975.awk) preserving the format:
END { if (f) print x }
$0 ~ v && f++
/^--/ { if (f) print x; x = f = 0 }
{ x = x ? x RS $0 : $0 }
  1. Run it like this:
nawk -f Hone1975.awk v="^IP=172.18.0.1$" list.txt