Split and rename the splitted files in shell scrip

2019-07-21 06:43发布

i used below command to spit the file

split -l 100 date.csv date.csv

The files are splittes as below

date.csvaa
date.csvab
date.csvac
date.csvad
date.csvae

I want to rename the files as below.

date_1.csv
date_2.csv
date_3.csv
date_4.csv
date_5.csv

please help

标签: linux shell unix
2条回答
我命由我不由天
2楼-- · 2019-07-21 07:33

this line does it in one-shot:

split --numeric-suffixes=1 --additional-suffix=.csv -l100 data.csv data_

little test (from stdin):

kent$  split --version|head -1
split (GNU coreutils) 8.22

kent$  l   
total 0

kent$  seq 10|split --numeric-suffixes=1 --additional-suffix=.csv -l2 - data_      

kent$  l
total 20K
-rw-r--r-- 1 kent kent 4 Mar 14 11:13 data_01.csv
-rw-r--r-- 1 kent kent 4 Mar 14 11:13 data_02.csv
-rw-r--r-- 1 kent kent 4 Mar 14 11:13 data_03.csv
-rw-r--r-- 1 kent kent 4 Mar 14 11:13 data_04.csv
-rw-r--r-- 1 kent kent 5 Mar 14 11:13 data_05.csv
查看更多
Viruses.
3楼-- · 2019-07-21 07:36

There are many ways of approaching this. Other answers provide a way to do it just with arguments passed to split - however the version of split on ubuntu 12.04 don't appear to support the arguments used in those answers.

Here is one. This splits the files and uses the default option on split to prefix the file names with an x. It then lists the files in order and renames them as required.

    split -l 100 date.csv
    i=1
    for x in `ls x* | sort`
    do
        mv $x date_$i.csv
        i=$(($i+1))
    done
查看更多
登录 后发表回答