[SOLVED] How to split a file according to new lines
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a file with a pattern like as below. As it is shown the file is contained two parts: header 1 and header 2. These parts are separated by a new line. I need to save each of one in a separated file such as header1, header2. How to split them by new line in linux?
It's easy enough to do in awk or perl. The former if the spacing in the headers are consistent, the latter if the spacing in the headers is more random (or if perl is more comfortable). Less than ten instructions more or less ought to do it in either.
Which do you prefer, awk or perl? Also, can you please show your script so far so we can see where you are at and which approach you are trying?
Last edited by Turbocapitalist; 09-22-2018 at 03:58 AM.
It's easy enough to do in awk or perl. The former if the spacing in the headers are consistent, the latter if the spacing in the headers is more random (or if perl is more comfortable). Less than ten instructions more or less ought to do it in either.
Which do you prefer, awk or perl? Also, can you please show your script so far so we can see where you are at and which approach you are trying?
I didn't write script, yet. But I prefer awk, do u know how to use it?
If headers are consistent and you wish to keep them into the new files, another approach with awk could be :
- Detect the begin header, use it to set the filename.
- Print all lines between the begin and end headers into a file with the filename set in previous instruction.
Awk can work on a range of lines surrounded by borders.
If headers are consistent and you wish to keep them into the new files, another approach with awk could be :
- Detect the begin header, use it to set the filename.
- Print all lines between the begin and end headers into a file with the filename set in previous instruction.
Awk can work on a range of lines surrounded by borders.
Code:
/beginning/,/ending/ {...}
I used it:
awk '/-----BEGIN/{flag=1}flag > file1;/-----END/{flag=0}' domain-crt.txt
but it doesn't separate the output in two separated files!
But you still need a file name. One way is to add another variable that gets incremented when the /begin/ pattern is found. Another way would be to extract a field from the line with the /begin/ pattern and use that. Store the name in a variable.
Either way you'll need to redirect to a file. Look in the manual for awk and scroll down to the explanation of the print function and look at the line with the redirect >
You can try redirecting everything to the same file first.
Then after that you can redirect to a file name stored in a variable.
Remember [code] [/code] tags when posting.
Last edited by Turbocapitalist; 09-23-2018 at 03:49 AM.
As Turbocapitalist said you have to redirect to a file. And one way or another, you have to define the filename.
I can not do more without giving you the answer :
Code:
awk '/begining/ { defining filename } /beginning/,/ending/ { writing line into your file }' domain-crt.txt
I can't understand! I'm stressful to do a task and I don't have much time to learn all things about awk!!! I just need to the exact answer!!
I should have 2 files taken one!!! like this:
Input file is:
---Begin---
ldsjflds
ldsf
---END---
---Begin---
ldsjfsdl
ldsjfs
---END---
You made it!
If you know awk better you can do
filename="file"++counter
and
flag{print > filename}
that would not print the stuff between the begin-end blocks.
If you do not make use of the flag then you don’t need to set it!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.