Find the answer to your Linux question:
Results 1 to 4 of 4
I am trying to write a Linux script to search through a list of header files, and then through a find, search each file for the header files (case insensitive ...
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    2

    Question Linux script to find and replace filenames

    I am trying to write a Linux script to search through a list of header files, and then through a find, search each file for the header files (case insensitive search). It will then replace the header name in the file with the correct case. Right now I am getting a sed error when it finds a match that has a newline character after it: "sed: -e expression #1, char 24: unterminated `s' command". Can anyone tell me how to fix this? Thanks!

    ~/header_list.txt
    -------------------
    release.h
    sys.h
    struct.h
    ...

    ~/fixcase_script.sh
    --------------------
    Code:
    #!/bin/sh
    #
    #Use this on the command line from inside the falcon3 folder to run this script
    #
    #find . \! -wholename '*/.svn/*' -a \( -name "*.h" -or -name "*.hpp" -or -name "*.cpp" -or -name "*.c" -or -name "*.cxx" -or -name "*.cc" -or -name ".idl" -or -name "Makefile" -or -name "*.pl" -or -name "*.py" \) -type f -exec ~/fixcase_script.sh {} \;
    
    currFile=$1
    
    echo "Current File: $currFile"
    
    for f in $(cat ~/header_list.txt)
    do 
    	grepMatch=$(grep -iow $f $currFile)
    	matchName=$grepMatch
    
    	base=${grepMatch##*/}
    	pathName=${grepMatch%${base}*}
    	ext=${grepMatch##*.}
    	escName=${base%.*}\\\.${ext}
    	fullName=${pathName}${escName}
    	
    	grepMatch=$fullName
    	grepMatchEnd=${fullName}\$
    
    	base=${f##*/}
    	pathName=${f%${base}*}
    	ext=${f##*.}
    	escName=${base%.*}\\\.${ext}
    	fullName=${pathName}${escName}
    
    
    	if [ "$grepMatch" != "" ] && [ "$grepMatch" != "\." ]
    	then
    		echo "Match: $matchName, Correct name: $f"
    		cat $currFile | sed s/"$grepMatch"$/"$fullName"/g > $currFile.new
    		mv $currFile.new $currFile
    	fi
    done
    Last edited by Kiniption; 10-10-2008 at 02:50 PM. Reason: added code tags

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    hi,

    instead of
    Code:
    sed s/"$grepMatch"$/"$fullName"/g
    try
    Code:
    sed "s/${grepMatch}$/$fullName/g"
    Linux and me it's a love story

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    2
    It didn't work Here is the output I got (it's the same as what I have been getting before).

    Code:
    $ find . -wholename './include/struct.h' -type f -exec ~/fixcase_script.sh {} \;
    Current File: ./include/struct.h
    Match: 63:reLEASe.h, Correct name: release.h
    
    Match: 59:sys.h, Correct name: sys.h
    
    Match: 5:struct.h
    49:struct.h, Correct name: struct.h
    
    sed: -e expression #1, char 27: unterminated `s' command
    Line 5 in struct.h looks like this:
    Code:
    // FILE:        struct.h

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    There are a few tools that you can use in your for loop. Just to give you a quick example:

    #!/bin/bash
    f="/tmp/test/system.h"
    path_name=`dirname $f`
    base_name=`basename $f`
    ext=`echo $basename | awk -F '.' '{print $NF}'`

    echo "file_name=$f"
    echo "path_name=$path_name"
    echo "base_name=$base_name"
    echo "ext=$ext"


    Also check if your version of sed if it supports option -i which replaces text in the file without outputting to a tmp file then renaming it back.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...