Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Chop lines from file 1

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
I have a file in the following format

a pool
SSC003 03 Available
SSC004 07 Available
b pool
SSC006 89 Available
SSC007 20 Available
---
---
---
---
c pool
SSC009 22 Available
---
---
---

What I need to do is chop out the different pools into seperate files, how would I do this with awk?

file1 =
a pool
SSC003 03 Available
SSC004 07 Available
b pool
SSC006 89 Available
SSC007 20 Available
---
---
---
---

file2 =
You get the idea I'm sure

regards Mike --
| Mike Nixon
| Unix Admin
| ----------------------------
 
Mike,

You've confused me about how you want the file split up. This assumes you want all lines from pool a up to pool b in file 1, pool b to pool c in file 2 etc.

{
if ($2=="pool") {
if (fn=="") close(fn)
fn = $2 $1 # or "file" ++ix
}
print > fn
} CaKiwi
 
Put it in a file, pool.awk say and enter

awk -f pool.awk your-file CaKiwi
 
Well spotted, dchoulette! I think I got a star under false pretenses! CaKiwi
 
The script would work fine except on some vesions of awk if you try to open a large number of files you would run out of file handles and get an error. CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top