1 Replies - 712 Views - Last Post: 06 June 2011 - 07:17 AM Rate Topic: -----

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Splitting files with BASH scripting

Posted 24 May 2011 - 10:42 AM

Hi everybody,
I'm trying to split a file based on context of certain lines, to transform it from this:

<random stuff>KEYWORD<random stuff>
more lines section 1
<random stuff>KEYWORD<random stuff>
more lines section 2
<random stuff>KEYWORD<random stuff>
more lines section 3



to this:
FILE 1
<random stuff>KEYWORD<random stuff>
more lines section 1

FILE 2
<random stuff>KEYWORD<random stuff>
more lines section 2

FILE 3
<random stuff>KEYWORD<random stuff>
more lines section 3



Basically, split a big file so any line that has the KEYWORD in it (anywhere in the line) is the first line of a new file. I want to number those files 1.txt, 2.txt, 3.txt, etc.

Since I'm going to be working with potentially 500+ files after the split, I can't use csplit, and that's the only unix utility I know that can do anything like this. Does anyone know of a possible 1 or 2 liner (or failing that, a short script) that can also do this? I'd appreciate any help.

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Splitting files with BASH scripting

#2 mufasa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 12-February 10

Re: Splitting files with BASH scripting

Posted 06 June 2011 - 07:17 AM

hopefully not grave digging, but have you looked at using a small bash script and just checking a line for your keyword/phrase? it may not be super efficient but it would work. you could check if the keyword existed by doing something like:

if [[ $line == *KEYWORD* ]]; then
     # do some fun stuff
fi


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1