简体   繁体   中英

Renaming files based on a string

i have a some files that needs to renamed with a string/pattern

actual file  : CCRD_LLX_814_20160218043477.EDI814


Rename files to: actual file. XHS.JOBRUNID.%INTERFACENAME%.actualfile.XHE

i have two types of INTERFACENAME:

 814 = EB_ENROLL_REQ
    810 = EB_BCHG_REQ

so if the actual file name has 814 in it then i have to rename the file as:

CCRD_LLX_814_20160218043477.EDI814.XHS.JOBRUNID.EB_ENROLL_REQ.CCRD_LLX_814_20160218043477.EDI814.XHE

if the actual file name has 810 in it then the file will be

CCRD_LLX_810_20160218043477.EDI814.XHS.JOBRUNID.EB_BCHG_REQ.CCRD_LLX_810_20160218043477.EDI814.XHE

Tried:

rename 's/^/Interface_Name/g' filename

But didnt worked out.please assist

A harmless bash script which does this simple task. This is based on the assumption that the last 3 characters in the filename determining which INTERFACE NAME to use.

#!/bin/bash

inputString=$1

# Bash built-in parameter substitution to get the last 3 characters from the string
pattern=${inputString: -3} 

declare -a myArray

myArray[814]=EB_ENROLL_REQ  # Mapping the INTERFACE NAME array
myArray[810]=EB_BCHG_REQ

mv -v "$inputString" "$inputString.XHS.JOBRUNID.${myArray[$pattern]}.$inputString.XHE"

Run the script as follows:-

$ ./rename_script.sh CCRD_LLX_814_20160218043477.EDI814
`CCRD_LLX_814_20160218043477.EDI814` -> `CCRD_LLX_814_20160218043477.EDI814.XHS.JOBRUNID.EB_ENROLL_REQ.CCRD_LLX_814_20160218043477.EDI814.XHE`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM