LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-03-2010, 04:57 AM   #1
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
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?
 
Old 12-03-2010, 05:49 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
1 members found this post helpful.
Old 12-03-2010, 05:52 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

And a sed solution:

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

Hope this helps.
 
1 members found this post helpful.
Old 12-03-2010, 06:05 AM   #4
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Original Poster
Rep: Reputation: 11
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.
 
Old 12-03-2010, 06:16 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Same idea as colucix, just a different take:
Code:
awk '(NR%10){printf "%s ",$0;next}1' file
 
Old 12-03-2010, 06:22 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
%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.
 
1 members found this post helpful.
Old 12-03-2010, 06:34 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,
Quote:
Originally Posted by Mark1986 View Post
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.
 
1 members found this post helpful.
Old 12-03-2010, 09:18 AM   #8
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Original Poster
Rep: Reputation: 11
Quote:
Originally Posted by grail View Post
%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.
 
Old 12-03-2010, 09:40 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Repeatedly getting command substitution: line 3: syntax error near unexpected token dontob Linux - General 8 08-31-2010 08:26 AM
how to copy some lines in a file and delete these lines after gartura Linux - General 1 07-20-2010 08:55 AM
How to grep lines containing a certain string PLUS the line following that line? kmkocot Linux - Newbie 5 09-01-2009 03:54 PM
Copy lines from console saidul Linux - Newbie 8 11-17-2008 03:16 PM
copy lines in vi editor jkmartha Linux - Newbie 4 05-14-2005 02:19 AM


All times are GMT -5. The time now is 09:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration