How to find pattern in file names?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find pattern in file names?
# 1  
Old 07-19-2010
How to find pattern in file names?

I have some files, those are abbreviated (ed,ea, and bi)
Code:
 
company_ed_20100719.txt
company_ea_20100719.txt
company_bi_20100719.txt

I would like to rename these files by replacing
Code:
 
ed with EmployeeDetails
ea with EmployeeAddress
bi with BankInfomration

as
Code:
 
company_ EmployeeDetails _20100719.txt
company_ EmployeeAddress _20100719.txt
company_ BankInfomration _20100719.txt

I want to write a shell script(ksh) that does the following

Code:
 
if [ file name contains ‘ed' ] then
 Replace ed with EmplaoyeeDetails
else if [file name contains ‘ea' ] then
Replace ea with EmplaoyeeAddress
else if  [file name contains ‘bi' ] then
Replace bi with BankInformation
fi

I am not sure what is the command/utility that helps my to serve similar to strstr function?
# 2  
Old 07-19-2010
Backup your data first!

Assuming an old ksh with no associative arrays:

Code:
for f in *_*_*.txt; do
  fp=${f%%_*} rst=${f##*_} 
  case $f in
    *_ed_* ) mv -- "$f" "$fp"_EmployeeDetails_"$rst";;
    *_ea_* ) mv -- "$f" "$fp"_EmployeeAddress_"$rst";;
    *_bi_* ) mv -- "$f" "$fp"_BankInfomration_"$rst";;
  esac
done

# 3  
Old 07-19-2010
it will take files 1 by 1 from your current directory and do the search and neccesary changes.

Code:
for filename in `ls *`
do
if [`grep ea "filename"`]
echo filename |sed 's/ea/EmployeeAddress/'
elif [`grep ed "filename"`]
echo filename | sed 's/ed/EmployeeDetails/'
elif [`grep bi "filename"`]
echo filename | sed 's/bi/BankingInformation/'
fi
done

but i will still prefer the case code that radulov has displayed above. its more robust than mine.
# 4  
Old 07-19-2010
MySQL

Code:
#!/bin/bash
my=("ed" "ea" "bi")
newmy=("EmployeeDetails" "EmployeeAddress" "BankInfomration")
ix=0
for i in ${my[@]}
  do
   myfile=$(ls -1 company* | grep $i)
   newfile=$(echo $myfile | sed "s/^\(.*_\).*\(_.*\)/\1 ${newmy[ix]} \2/")
   mv -v "$myfile" "$newfile"
   ((ix++))
  done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Beginners Questions & Answers

Consternation of multiple file names based on naming pattern

Hi, I have the following reports that get generated every 1 hour and this is my requirement: 1. 5 reports get generated every hour with the names "Report.Dddmmyy.Thhmiss.CTLR" "Report.Dddmmyy.Thhmiss.ACCD" "Report.Dddmmyy.Thhmiss.BCCD" "Report.Dddmmyy.Thhmiss.CCCD"... (1 Reply)
Discussion started by: Jesshelle David
1 Replies

3. Shell Programming and Scripting

Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (4 Replies)
Discussion started by: shadow_fawkes
4 Replies

4. Shell Programming and Scripting

**URGENT ** : Comparing the pattern of the file names in 2 different directories

Hi, I have got a requirement for which i need your help. The following problem is required to get solved in PERL SCRIPT. Here is the requirement. There are 4 folders say SRC_DIR1, SRC_DIR2 and TGT_DIR_1,TGT_DIR_2 (Note: both path of SRC_DIR1 & SRC_DIR2 are different but both path of... (1 Reply)
Discussion started by: shadow_fawkes
1 Replies

5. Shell Programming and Scripting

Identify file name pattern in different file names

Hi, need help in recognizing the pattern of file name. For e.g. file name 1: <static file prefix>.<store cd>_<YYYYMMDD>.<ext> file name 2: <static file prefix>_<YYYYMMDD>.<ext> I want to know that there are 3 dots "." in the file name1 and one dot "." in file name2. How can I know... (3 Replies)
Discussion started by: dips_ag
3 Replies

6. Shell Programming and Scripting

find specific file names and execute a command depending on file's name

Hi, As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two... (3 Replies)
Discussion started by: armando110
3 Replies

7. Shell Programming and Scripting

Split File by Pattern with File Names in Source File... Awk?

Hi all, I'm pretty new to Shell scripting and I need some help to split a source text file into multiple files. The source has a row with pattern where the file needs to be split, and the pattern row also contains the file name of the destination for that specific piece. Here is an example: ... (2 Replies)
Discussion started by: cul8er
2 Replies

8. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

9. Shell Programming and Scripting

Remove repeating pattern from beginning of file names.

I want a shell script that will traverse a file system starting at specific path. And look at all file names for repeating sequences of and remove them from the file name. The portion of the name that gets removed has to be a repeating sequence of the same characters. So the script would... (3 Replies)
Discussion started by: z399y
3 Replies

10. UNIX for Advanced & Expert Users

Find File names with sustitution

Hi All, Iam trying to find two kinds of files while ignoring rest of the files in a directory The files are like below Files to be found -------------------- perp45560 oerp4556 Files to be ignored ---------------------- oerp4556123450 oerp4556123470 I was trying the following... (4 Replies)
Discussion started by: baanprog
4 Replies
Login or Register to Ask a Question