LinuxQuestions.org
Help answer threads with 0 replies.
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, 06: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, 07:15 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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-06-2009, 12:42 AM   #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, 01:44 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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


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


All times are GMT -5. The time now is 02:28 AM.

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