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 have one file with 100 random chosen lines using the shuf command. I would like to paste every 10 lines right after each other. So lines 1 - 10 become line 1, lines 11 - 20 become line 2, etc.
I am using Cygwin at the moment, so all must be done by command line. (Reason is; this is my laptop at work, which I am not allowed to dual boot or anything.)
I found the sed command, but I fail to see where I can use it to do what I want to achieve. Any suggestions?
Thank you so much guys, both commands worked. The sed command is a tad more readable for me, though I can't figure out the meaning of the s and the /g.
In case of the awk command %s seems to look like a new input line, and $0 assumes first column. NR stands for new line, using Linux standards. I don't understand why NR % 10 needs to be equal to 0 in order to print a \n. My own logic would say it needs to be equal to 1 (1 meaning true and 0 meaning false).
% - modulo gives you the left over after being divided, hence if NR = 12 then NR % 10 = 2
Yes 1 (actually non-zero) is true and 0 is false however the result of NR % 10 for all lines not a multiple of 10 will be a value other than zero hence true, but we only want to print
a new line after the 10th line has been read. So you can do one of the following:
The sed command is a tad more readable for me, though I can't figure out the meaning of the s and the /g.
The N;N;N .... loads the lines into the buffer. The s/\n/ /g replaces the \n (=newline/carriage return) to a space and does this for all instances (not only the first one).
% - modulo gives you the left over after being divided, hence if NR = 12 then NR % 10 = 2
Yes 1 (actually non-zero) is true and 0 is false however the result of NR % 10 for all lines not a multiple of 10 will be a value other than zero hence true, but we only want to print
a new line after the 10th line has been read. So you can do one of the following:
Code:
(NR % 10) == 0
OR
!(NR % 10)
Both of these statements will evaluate to true.
Hope that helps.
Ok, I think I've got this now. Using (NR % 10) you are actually dividing the row count with 10. And if the left-over equals 0, then you return TRUE. So if you came to line 20 (for some reason skipped line 10), there would also be a \n.
Why didn't you use something like NR = 10, just counting with +1 up to 10 and then put a \n?
The sed command seems somewhat clearer now. I just found the s/ to be an operand, which I at first thought was part of the N;N; line. The g/ makes sure this is done untill end of file or something (man pages says it holds/ appends space to pattern space).
I hope I made the right assumptions. Correct me if I'm wrong. Also thank you very much again for your replies.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.