LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Copy 10 lines to 1 line, repeatedly (https://www.linuxquestions.org/questions/linux-newbie-8/copy-10-lines-to-1-line-repeatedly-848118/)

Mark1986 12-03-2010 04:57 AM

Copy 10 lines to 1 line, repeatedly
 
Hi,

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?

colucix 12-03-2010 05:49 AM

Maybe something like:
Code:

commands | awk '{printf "%s ",$0};(NR % 10)==0{printf "\n"}'
this prints each records on the same line separated by space and put a newline every 10 records. Hope this helps.

druuna 12-03-2010 05:52 AM

Hi,

And a sed solution:

sed 'N;N;N;N;N;N;N;N;N;s/\n/ /g' infile > outfile

Hope this helps.

Mark1986 12-03-2010 06:05 AM

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).

Correct me if I'm wrong.

grail 12-03-2010 06:16 AM

Same idea as colucix, just a different take:
Code:

awk '(NR%10){printf "%s ",$0;next}1' file

grail 12-03-2010 06:22 AM

%s - Stands for string which in this case is $0

$0 - is the entire line

NR - Number Records read from file

% - 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.

druuna 12-03-2010 06:34 AM

Hi,
Quote:

Originally Posted by Mark1986 (Post 4179347)
Thank you so much guys, both commands worked.

You're welcome :)

Quote:

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).

Hope this helps.

Mark1986 12-03-2010 09:18 AM

Quote:

Originally Posted by grail (Post 4179367)
%s - Stands for string which in this case is $0

$0 - is the entire line

NR - Number Records read from file

% - 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.

grail 12-03-2010 09:40 AM

Quote:

NR = 10
I assume test == but this will only ever happen once as NR always increases based on number of records in the file(s)


All times are GMT -5. The time now is 11:32 PM.