Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to split and reorganize files in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to split and reorganize files in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to split and reorganize files in Linux".

Use csplit to split files

Csplit is one of these interesting little commands. It's always with you, and once you start using it, you can't do without it. Csplit splits a single file into multiple files. This example demonstrates the simplest use, dividing the file foo.txt into three files, with line numbers 17 and 33 as split points:

$csplit foo.txt 17 33259138892359

Csplit creates three new files in the current directory and prints out the size of the new file in bytes. By default, each new file is named xx_nn:

$lsxx00xx01xx02

You can use the head command to view the first ten lines of each new file:

$head xx*== > xx00 xx01 xx02

What if you want to split the file into multiple files that contain the same number of lines? You can specify the number of lines, and then place the number of repeats in curly braces. This example repeats the split 4 times and dumps the rest to the last file:

$csplit foo.txt 5 {4} 57148824918663798

You can use asterisk wildcards to tell csplit to repeat the split as much as possible. This sounds cool, but if the file is not equally divided, it may fail (LCTT: lower versions of csplit do not support this parameter):

$csplit foo.txt 10 {*} 1545211518481901csplit:'10 percent: line number out of range on repetition 41430

The default behavior is to delete the output file when an error occurs. You can use the-k option to solve this problem, and when there is an error, it will not delete the output file. Another behavior is that every time you run csplit, it will overwrite the previously created files, so you need to save them separately with a new file name. Use-prefix= prefix to set a different file prefix:

$csplit-k-- prefix=mine foo.txt 5 {*} 5714882491866993csplit: '5clients: line number out of range on repetition 9437 $lsmine00mine01mine02mine03mine04mine05

The option-n can be used to change the number of digits in which files are numbered (2 digits by default):

$csplit-n 3-- prefix=mine foo.txt 5 {4} 571488249186613813798 $lsmine000mine001mine002mine003mine004mine005

The "c" in csplit means context. This means that you can split the file according to any matching method or clever regular expression. The following example divides the file into two parts. The first file ends at the line before the first occurrence of "fie", and the second file begins with the line containing "fie".

$csplit foo.txt / fie/

Split the file each time "fie" appears:

$csplit foo.txt / fie/ {*}

Split the file in the first five occurrences of "fie":

$csplit foo.txt / fie/ {5}

Copy only if the content begins with a line that contains "fie", and omits everything preceding it:

$csplit myfile fie% splits files into different sizes

Split is similar to csplit. It divides files into specific sizes, which is great when you split large files into small multimedia files or use network transfer. The default size is 1000 rows:

$split foo.mv$ ls-hl266K Aug 21 16:58 xaa267K Aug 21 16:58 xab315K Aug 21 16:58 xac [...]

They are similar in size, but you can specify any size you want. In this example, it is 20m bytes:

$split-b 20m foo.mv

The unit of size is abbreviated as K ·M ~ Q G ~ T ~ P ~ E ~ Z ~ Y (power of 1024) or KB,MB,GB and so on (power of 1000).

Choose your own prefix and suffix for the file name:

$split-a 3-numeric-suffixes=9-additional-suffix=mine foo.mv SB240K Aug 21 17:44 SB009mine214K Aug 21 17:44 SB010mine220K Aug 21 17:44 SB011mine

The-an option controls the numeric position of the number. -numeric-suffixes sets the starting value of the number. The default prefix is x, or you can set a different prefix by typing it after the file name.

Merge split files

You may want to reorganize your files at some point. The commonly used cat command is used here:

$cat SB0* > foo2.txt

The asterisk wildcards in the example will match all files that start with SB0, which may not get the results you want. You can use question mark wildcards to match more accurately, using one question mark for each character:

$cat SB0? > foo2.txt

As always, refer to the relevant manuals and information pages for complete command options.

The above is all the contents of the article "how to split and reorganize files in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report