Case insensitive file name search and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case insensitive file name search and replace
# 1  
Old 03-10-2015
Case insensitive file name search and replace

I am trying to find case insensitive file names and then replace that particular file with other name.

Code:
if [ -f `find -iname *update* -type f -printf "%f\n"` ]
then
        ls  | grep -i "update" | xargs -I {} mv {} LineItems.csv
        echo "File moved from *update*"
elif [ -f `find  -iname *priority* -type f -printf "%f\n"` ]
then
        ls  | grep -i "priority" | xargs -I {} mv {}  LineItems.csv
        echo "File moved from *priority*"
else
        echo " LineItems file doesnt exist"
        exit 1
fi

Original File name is PRioriTY.csv.. should be changed to LineItems.csv..
but always its going inside the first loop of update.
# 2  
Old 03-10-2015
I guessing you are using find only for the case insensitive option and you are not really interested in files within sub-directories of the current location.

With bash you could achieve this with:

Code:
#!/bin/bash

for file in *
do
   case "${file,,}" in
      *priority*)
               mv "$file" LineItems.csv
               echo "File moved from *priority*"
               break ;;
      *update*)
               mv "$file" LineItems.csv
               echo "File moved from *update*"
               break ;;
   esac
done

The reason I use break above is that if you have two or more matching files in your folder all but the last one processed will be lost. Break causes only 1 file to be moved to LineItems.csv at a time, the presumption here is that LineItems.csv is then copied elsewhere or processed in some way, before this loop is called again.

With a shell that doesn't support ${var,,} you could try these alternatives:

Code:
   case "$file" in
      *[Pp][Rr][Ii][Oo][Rr][Ii][Tt][Yy]*)

Code:
small_name=$(echo $file|tr '[A-Z]' '[a-z]')

case "$small_name" in
     *priority*)

~

Last edited by Chubler_XL; 03-10-2015 at 06:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to search case insensitive

Hello , Using the below scrip to search a string in a file , by case-insensitively Please assist on using the toupper() as getting error !. #!/usr/bin/ksh set -x curr_dir=`pwd` file_ctr=0 printf "\n Reviewing the output file from the directory: %s \n\n" $curr_dir ls -latr ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. UNIX for Dummies Questions & Answers

Command for a case insensitive search

Hi All, What is the command to search a file for a case-insensitive match 1.grep -nc text filename 2.grep -i text filename 3.grep -i filename text 4.grep -nc filename text 5.grep -c text filename Thanks for your help (1 Reply)
Discussion started by: bobby1015
1 Replies

3. UNIX for Dummies Questions & Answers

Using FIND with case insensitive search

I am using HP-Unix B.11.31. Question: How to do the case insensitive search using FIND? Example: I would like list the files with extension of *.SQL & *.sql. When I try with command find . -type f -name *.sql, it does not lists file with *.SQL. (5 Replies)
Discussion started by: Siva SQL
5 Replies

4. UNIX for Dummies Questions & Answers

Using sed for case insensitive search

Hi, I have a file named "test_file" that has the below content. It has words in upper/lower cases PRODOPS prodOPS ProdOps PRODops escalate Shell My requirement is to replace all the "prodops" (what ever case it may be) with "productionoperations". I tried using the "i" option with... (7 Replies)
Discussion started by: sbhuvana20
7 Replies

5. AIX

Case insensitive search in AIX man ?

Hello, Linux man command search is case insensitive by default, but not AIX man. How do I serch case insensitive while using AIX manual pages ? thanks Vilius (7 Replies)
Discussion started by: vilius
7 Replies

6. UNIX for Dummies Questions & Answers

more command case insensitive search ?

Hello, How do I set case insensitive search mode while the file is open with more command ? (I know -i option which could be used before opening) thanks Vilius (2 Replies)
Discussion started by: vilius
2 Replies

7. Shell Programming and Scripting

How to replace a string (case insensitive)?

Hi, Please help me in following prob. I have a input file in which UPDATE statements will be present. I need to check the count of the rows impacted by each statement. I am using below code to do so: $dml --> File having UPDATE SQLs like Update <table_name> Set <field>=<value>... (3 Replies)
Discussion started by: ustechie
3 Replies

8. Shell Programming and Scripting

Case Insensitive search

Hey , i am trying to do a search for the certain books , and im trying to make it case insensitive. what i have come up with so far is this : Database.txt RETARDED MONKEY:RACHEAL ABRAHAML:30:30:20 GOLD:FATIN:23.20:12:3 STUPID:JERLYN:20:40:3 echo -n "Title: " read Title echo -n... (3 Replies)
Discussion started by: gregarion
3 Replies

9. Shell Programming and Scripting

case-insensitive search with AWK

Hi All, How we can perform case-insensitive search with AWK.:rolleyes: regards, Sam (11 Replies)
Discussion started by: sam25
11 Replies
Login or Register to Ask a Question