LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 07-05-2009, 05:21 AM   #1
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Rep: Reputation: 15
Understanding how csplit works


Hi,
I tried to split a file into 3 pieces. Following is what I did:

Code:
#ls -lh *
-rw-r--r--  1 root root 156K Jul  5 13:23 file1.txt
#wc -l file1.txt
4617 file1.txt
#csplit file1.txt 10 {3}
279
398
390
310
157767
#ls -lh *
-rw-r--r--  1 root root 156K Jul  5 13:23 file1.txt
-rw-r--r--  1 root root  279 Jul  5 14:14 xx00
-rw-r--r--  1 root root  398 Jul  5 14:14 xx01
-rw-r--r--  1 root root  390 Jul  5 14:14 xx02
-rw-r--r--  1 root root  310 Jul  5 14:14 xx03
-rw-r--r--  1 root root 155K Jul  5 14:14 xx04
#cat xx00 | wc -l
9
#cat xx01 | wc -l
10
#cat xx02 | wc -l
10
#cat xx03 | wc -l
10
#cat xx04 | wc -l
4578
Here are my questions:
1) Why the command generated 5 files, although I put 3 in the braces?
2) Why the first file (xx00) contains 9 lines, although I specified to split the file into 10-line-size sections?

Thanks in advance.
 
Old 07-05-2009, 06:15 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
From the csplit man page:
Code:
Each PATTERN may be:

       INTEGER
              copy up to but not including specified line number
       {INTEGER}
              repeat the previous pattern specified number of times
Your command copy up to (but not including) the 10th line and repeat this three times. So your final result is 1 file of 9 lines, 3 files of 10 lines, 1 file with the rest. I don't know why the three files have 10 lines instead of 9, but the result is quite consistent with the command you used. What is the result you want to achieve? Maybe split -l is more useful.
 
Old 07-05-2009, 11:42 PM   #3
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
Hi colucix,

In my command, the INTEGER=10 and
the {INTEGER}={3}

Therefore, expect to see fours files, not five. Also, I expect all of them should be of same number of lines.
 
Old 07-06-2009, 12:44 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You can't split a file with 4617 lines in 4 pieces of 10 lines each! The rest remains in its own fragment. If you want an equal number of lines, try split, since the csplit command is more useful when you want to split a file based on some pattern.
 
  


Reply



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
Strangest FTP error on RHEL5. gFTP works. RHEL4 works too. GuitsBoy Linux - Software 1 04-10-2008 08:29 AM
Mounting works, playing music works, reading tags doesn't Celettu Linux - Newbie 7 08-23-2006 12:27 PM
csplit problem ? :( PB0711 Linux - Software 0 02-23-2006 08:21 PM
XAWTV works, videodog works, motion works but how to code my own? rylan76 Linux - Hardware 0 01-06-2006 06:30 AM
understanding how configure works msteudel Linux - Software 3 12-20-2004 04:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:57 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
Open Source Consulting | Domain Registration