teepu teepu - 1 month ago 18
Bash Question

split command with user defind prefix and suffix

I have a file

file_Md.csv
with more than
5000
lines in it. So I wanted to split the original file into multiple files with
2000
lines each. But the result files should have prefix as
file_Md
and suffix as
.csv
.

Example
file_Mda.csv
,
file_Mdb.csv
like wise.

So for this I used the below command to prefix
split -l 2000 -a 1 file_Md.csv file_Md


Could any one help me with suffix part?

PS. PS.
Answer

Assuming your split is not having --additional-suffix option available ,here is one awk solution:

General solution: for number increment: User need to add the value of prefix ,suffix and number of lines to do partition with.

awk -v N=1 -v pre="file_Md" -v suf=".csv" -v line=4000 'NR%line==1{x=pre N suf ;N++} {print > x}' filename

Genreral solution for alphabets increment:

awk -v pre="file_Md" -v suf=".csv" -v line=4000 'BEGIN{split("ABCDEFGHIJKLMNOPQRSTUVWXYZ",A,"");} NR%line==1{x=pre A[N] suf ;N++}{print > x}' filename

Hardcoded solution for your case:

awk 'BEGIN {  split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", A, "");} NR%4000==1{x="file_Md" A[N] ".csv";N++}{print > x}' file_Md.csv 

Example: Files before running the command:

 ls -1
total 28K
file_Md.csv 

Files after Executing the command:

sh-4.1$ ls -1
file_Md.csv  # this is orignal file
file_MdA.csv
file_MdB.csv
file_MdC.csv
file_MdD.csv
file_MdE.csv

If you just want to increment by numbers:

awk -v N=1 'NR%4000==1{x="file_Md" N".csv";N++} {print > x}' file_Md.csv

Explanation : NR%4000 will find out the lines which are divisible by 4000. So take further action of them. Define a variable x which is set to "file_Md" then a[N] and finally .csv,which is generated from split function.
split function is splitting A-Z into individual letters. It would be lot simple if you use number instead of Alphas.