0

I have a huge .txt file with multiple lines, but there's a particular string on a line that separates a number of lines from one another

I want to use that string to split into numerous files named the current string.

Example of specific string:

 Found matches in (anything can be here):

Example of data in huge .txt:

 Found matches in (anything can be here):
 ..............
 ..............
 ..............
 ..............
 ..............
 ..............

 Found matches in EXAMPLE:
 ..............
 ..............
 ..............
 ..............
 ..............
 ..............

Desired output:

 Found matches in (anything can be here).txt < contains data below its self but before another line stating " Found matches in

 Found matches in EXAMPLE.txt < contains data below its self but before another line stating " Found matches in

Also it's possible that there could be duplicate " Found matches in (anything can be here)" lines with data, so rather than overwrite the current, is it possible to rename (1) and (2) and (3) etc or just simply ADD too the current file ( not overwrite)

  • 2
    Great. So what have you tried, and where are you stuck? – maulinglawns Aug 20 '18 at 19:47
  • @maulinglawns in all honesty I'm new to bash, I wouldn't even know where to begin, sorry I know this place is for learning but any direction would help. – questionaires Aug 20 '18 at 19:48
1

How far would

awk '/^ *Found matches/ {FN = $0 ".txt"; next} {print >> FN}' file

get you?

EDIT: corrected version ("Found matches" NOT in first line):

awk '/^ *Found matches/ {if (FN) close (FN); FN = $0 ".txt"; next} FN {print >> FN}' file
  • awk: cmd. line:1: (FILENAME=- FNR=1) fatal: expression for `>>' redirection has null string value – questionaires Aug 20 '18 at 20:43
0

in all honesty I'm new to bash, I wouldn't even know where to begin, sorry I know this place is for learning but any direction would help

You do not have to apologize for not knowing. Nobody is born proficient in writing scripts.

However, as your question stands right now, it is not so much asking for help, but rather a ”please write a script for me that solves X and Y”.

Those questions are generally frowned upon by this (and most other) GNU/Linux communities.

But, back to the problem at hand. The fact that you do not even know where to begin. Here is how I started out, several years ago:

  • I read this guide (and did all the exercises)
  • Then I read this guide (and did all the exercises)
  • I bought this book. Read it, and did most of the exercises
  • I got involved with the community (at first as an observer)

Though the most important and useful step I took was that I used GNU/Linux for everything. Meaning; GNU/Linux became my one and only operating system, thus forcing me to solve whatever issue I faced, using the skills I had, or forcing me to learn new skills*. When faced with the latter, I often turned to the community for help - which also taught me (I hope!) how to formulate good questions.

One of the many great things with GNU/Linux is that there is no ”red tape” stopping you in the form of licensing and/or high cost of software. You are free to tinker with whichever distribution of your choosing. Do that, and you will become proficient enough to solve not only the problem posed in your question, but many others.

Now, I am fully aware that my route might not fit everybody. You should of course choose whatever path to learning that best suits you.

Finally, I sincerely hope this does not sound condescending, my intention is the exact opposite; to get you more interested and to dig deep in the wonderfully geeky world of GNU/Linux.

Happy Hacking!


* For you youngsters out there; hacking Xorg, and getting both audio and WiFi working was not a walk in the park using GNU/Linux around ten years ago (and don't get me started on bluetooth!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.