[SOLVED] move a line with either sed/awk/perl script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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 think you need to be a bit more detailed about your requirements. Do you only need to specify the line numbers to combine, or do you need to match certain text strings? Is it only concatenating sequential lines, or will you have a need to combine lines that are separated by something else?
Taking the opening post at face value, you can do this with sed:
Code:
sed -e '2 {N;s/\n/ /}' -e '5 {N;s/\n/ /}' file.txt
Each expression '-e' will take the line number you specify, combine it with the following line and then replace the newline separating them with a space. You could easily change the line number to a text match if necessary by replacing the line number with /textstring/.
Last edited by David the H.; 07-02-2009 at 12:23 PM.
'Dave the H.' hit the nail on the head. I need it to match the string test2. When I try it with his string, the output is correct. However, when I change it to look for the actual text, it is erroring.
Code:
sed -e 'test2 {N;s/\n/ /}' -e 'test5 {N;s/\n/ /}' junk1
sed: can't find label for jump to `est5'
So I think that the search string might need to be a little different.
The whole point is to search for a string. If the string is found, append the entire line to the end of the previous line.
Sorry for being generic.
Last edited by don_wombat_73; 07-02-2009 at 01:09 PM.
Look closer at what I wrote. You need to enclose the text in slashes '/test2/'. This turns it into a regular expression string, which will match if it appears anywhere in the line.
edit:
By the way, "If the string is found, append it to the end of the previous line" is a little more difficult than appending the following line to the one that matches. Which one do you really want?
Last edited by David the H.; 07-02-2009 at 01:09 PM.
Whatever would be the easiest I guess. I need to move lines around so that the end result would be a line looking like
Code:
test2 test3
The example works fine with /textstring/. But lest say the test2 was 'test2foofoofoofoo' and I only wanted to search for test2. Would I use a wildcard? /test2*/
Last edited by don_wombat_73; 07-02-2009 at 01:26 PM.
The search will match partial strings. Add a dollar sign to the end of the text string if you don't want it to match a partial string. '/test2$/'
In regular expressions, ^=startofline and $=endofline, so if you wanted to match a whole line you'd need to use '/^test2$/'.
And no, * wouldn't work. Its regex meaning is slightly different from the familiar globbing wildcard. In regex it means "match the preceding character zero or more times". To simulate the * wildcard in regex, you'd have to use ".*", where the period means "any character".
(edit: actually, it looks like * alone does work in this case. I guess sed can handle it either way here.)
There are many good regex tutorials on the net. I recommend reading up on it.
Last edited by David the H.; 07-02-2009 at 02:26 PM.
Glad to see you got it working. I notice two things to be cautious about though.
'/^host/' will match every line that starts with 'host', no matter what else follows.
's/\n//' will simply delete the line-break. The two lines will run together with no space between them.
I hope these are what you want.
BTW, you can now mark your threads as "solved", so that future readers will know you got a satisfactory answer. It's in the thread tools box. You may want to use it to mark this one.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.