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 ...
- 10-10-2008 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 2
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 doneLast edited by Kiniption; 10-10-2008 at 02:50 PM. Reason: added code tags
- 10-12-2008 #2
hi,
instead of
tryCode:sed s/"$grepMatch"$/"$fullName"/g
Code:sed "s/${grepMatch}$/$fullName/g"Linux and me it's a love story
- 10-14-2008 #3Just 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).
Line 5 in struct.h looks like this: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
Code:// FILE: struct.h
- 10-15-2008 #4Linux 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.


Reply With Quote