Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Sed reads and operates on lines of text. For 3 lines to be in the line buffer, You need to use the `N' command or add a line to the hold buffer with the `H' command.
# Firstly, create a data file
$ cat >data.dat <<EOF
one
key1
two
key2
three
EOF
$ sed -n '/^key1/ {N; N; s/^\(key1[^\n]*\)\n[^\n]*\n\(key2[^\n]*\)$/\1 \2/p }' data.dat
key1 key2
Happy with ur solution... Tick "yes" and mark as Solved!
Last edited by dru8274; 07-10-2012 at 07:16 AM.
Reason: Add
It helped. Now I can merge those three lines together, but somehow this is the only line in my result, now I need the rest of the file to remain untouched.
So I try to remove the search part /^key1/, but my result becomes empty.
sed is not well-designed for multi-line editing. It normally takes one line at a time into its edit buffer, minus the newline, applies the given expressions to it, and then re-adds the newline to any output afterwards.
For something like this you have to tell it to grab and store multiple lines at once with the "N" command (or perhaps by use of the hold buffer). Only then will there be literal newlines in the buffer for you to work with.
You'll also need to process the lines in a loop if you need to catch multiple sequential occurrences, as it replaces the whole buffer after each operation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.