Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split file based on the number of X symbol on each line

I’m not sure if this is possible, but I’m wondering if it’d be able to split a file into multiple files – dependent on the amount of a specified character there is on each line.

Lets use a colon (:) as an example

File.txt contains the following data (example):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Stack:Overflow   
Stack:Overflow:Flow    
Stack:Over:Flow:Com

Entire line containing 1 colon, goes to 1.txt
Entire line containing 2 colons, goes to 2.txt
Entire line containing 3 colons, goes to 3.txt

(And of course) there wouldn’t be a limit to the amount of colons, and format may not necessarily always match the exampled pattern.

Sorry if this is a vague question, I’m first time posting on StackOverflow in a long.


Another side question:
Inserting a specific character between 2 different regexs.

Data:

Stack@Stack.Stack192.168.0.1

I’m trying to insert a delimiter which will be ":"
Between 2 different regexes.
Regex #1 being: [A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}
Regex #2 being: [0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}\.[0-9]{1,4}

So the desired output would be:

Stack@Stack.Stack:192.168.0.1

>Solution :

With GNU AWK this approach will provide your expected outcome:

awk -F":" '{print > ((NF - 1)".txt")}' file.txt

NB. if you have a large number of delimiters (hundreds – thousands) you may also run into trouble for having too many open files (I believe ulimit -n will tell you how many different files you can have open at one time; on my system it’s 256)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading