0

I have basically splitted one file into 8 files.

DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat  #Original File

I got 8 new files as below filename.

DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.aa
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.ab
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.ac
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.ad
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.ae
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.af
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.ag
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_2_20170502094536.dat.ah

I want to rename these files as below name:

DA_TRP_TOTAL-SHIPPED_v1_DNH_1_8_20170502094536.dat
DA_TRP_TOTAL-SHIPPED_v1_DNH_2_8_20170502094536.dat
DA_TRP_TOTAL-SHIPPED_v1_DNH_3_8_20170502094536.dat
DA_TRP_TOTAL-SHIPPED_v1_DNH_4_8_20170502094536.dat
DA_TRP_TOTAL-SHIPPED_v1_DNH_5_8_20170502094536.dat
DA_TRP_TOTAL-SHIPPED_v1_DNH_6_8_20170502094536.dat
DA_TRP_TOTAL-SHIPPED_v1_DNH_7_8_20170502094536.dat
DA_TRP_TOTAL-SHIPPED_v1_DNH_8_8_20170502094536.dat

How can I do it in unix shell scripting?

2

The recommended tool for rename bunches of files is rename, this will do your job.

rename -n 'our $i++; s/_DNH_\K\d+_\d+(.*)\..*/${i}_8$1/' DA*.dat.*
0

You could select right tool at first to spiting.

split -a 1 --numeric-suffixes=1 -n 8 myfile myFile_from_8_

this will split myFile into the 8 parts with name starts with myFile_from_8_N (The N is from 1-8)

Or even you can simulate the split with awk command as like below with X number of records in each split-ed files (in sample X=100, means 100 records per files):

awk 'NR%100==1{ file=sprintf("myFile_from_8_%d",((++c)) )} {print >file}' myFile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.