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? |
Maybe something like:
Code:
commands | awk '{printf "%s ",$0};(NR % 10)==0{printf "\n"}' |
Hi,
And a sed solution: sed 'N;N;N;N;N;N;N;N;N;s/\n/ /g' infile > outfile Hope this helps. |
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. |
Same idea as colucix, just a different take:
Code:
awk '(NR%10){printf "%s ",$0;next}1' file |
%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 Hope that helps. |
Hi,
Quote:
Quote:
Hope this helps. |
Quote:
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. |
Quote:
|
All times are GMT -5. The time now is 11:32 PM. |