我正在使用Perl替代函数,s ///
我的理解是第一个参数是正则表达式。我不希望第一个参数成为正则表达式。我希望它是一个字符串。我想要转义/,tab和换行符等字符。
问题是我有一个文件。我想将跨越多行的文本块更改为不同的文本块。文本中可能包含/,逗号,#,制表符和换行符。我已经转义/到/,选项卡到输入文本中的\ t和换行符\ n。
我在bash脚本中运行。更改行是:
perl -0777pe "s/# Options\n\t-stdin/# Options\n\t-stdout/g" "$file" >/home/me/"${filename}"
实际输入文本要长得多,例如。忽略所有尾随空格。 findString是一个strng。
# Usage: \/Users\/mac\/BitHoist-PPC-MacOS-X [options] input... < input > output\n
# Options and inputs may be intermixed\n
\t-stdin # Use standard input as input file\n
\t-offset nn # Offset next input file data by nn\n
\t # Applies to next input specification
答案 0 :(得分:1)
你可以通过简单的引用来使用String而不是普通的正则表达式,例如: S / \ Q $字符串/.../
答案 1 :(得分:1)
斜杠,逗号,制表符(HT)和换行符(LF)在正则表达式中并不特殊。它们中的每一个都匹配相同的字符作为目标字符串的一部分。
答案 2 :(得分:0)
此Bash脚本允许您更改.c和.h文件中的多行字符串。它包括我在这里询问的perl替代语句。
#!/bin/bash
#-vx
# textChangeMultLine.bash
#
# Changes multiple lines of .c and .h text files in a directory.
# You will need to adjust the input directory and output directeries as needed
# inputDir
# outputDir
# Only changed files are written out to the new directory
#
# The string to change is ${findString}. They may contain line end characters.
#
# The new string will be ${replaceString}
#
#
# Copyright (C) 2014 Historystamp
#
# textChangeMultLine.bash is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# textChangeMultLine.bash is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BitHoist; see the file License. If not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA.
#
#
# debug info
export PS4='+(${BASH_SOURCE}:${LINENO}):'
# trace all the lines
#set -o xtrace
inputDir="/home/me/BitHoist/source"
outputDir="/home/me/BitHoist/test/iout/"
veryVerbose="no"
# Check for command-line options used when calling the script
if [ $# -gt 0 ] ; then
while getopts "f:vh" Option ;
do
case "${Option}" in
f ) inputDir=${OPTARG}
;;
h ) echo "Help"
echo "-f <filename> source directory to read."
echo " You may want to specify the complete path."
echo " default is ${inputDir}"
echo " by the way output is ${outputDir}"
echo "-h Help which you see"
echo "-v verbose messages."
echo
exit 10
;;
v ) echo "Will display very detailed messages. -v argument used on command line."
echo
veryVerbose="Yes"
;;
* ) echo "Unknown argument among arguments $* on command line. Try -h"
exit 11
;;
esac
done
fi
# string to find.
read -d '' findString <<"EOFEOFEOF"
BitHoist is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2,
as published by the Free Software Foundation.
BitHoist is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BitHoist; see the file License. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA.
EOFEOFEOF
if [ "${veryVerbose}" = "Yes" ]; then
echo "findString before changes is:"
echo -n "${findString}" | hexdump -C
fi
# escape text for Bash.
# change / to //
findString=${findString//\\/\\\\/}
# escape "
findString=${findString//\"/\\\"/}
if [ "${veryVerbose}" = "Yes" ]; then
echo "changed findString is:"
echo -n "${findString}" | hexdump -C
fi
# string to replace
read -d '' replaceString <<"EOFEOFEOF"
GNU General Public License
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
For a copy of the GNU General Public License
see <http://www.gnu.org/licenses/>.
EOFEOFEOF
if [ "${veryVerbose}" = "Yes" ]; then
echo "replaceString before changes is:"
echo -n "${replaceString}" | hexdump -C
fi
replaceString=${replaceString//\"/\\\"/}
replaceString=${replaceString//\\/\\\\/}
if [ "${veryVerbose}" = "Yes" ]
then
echo "changed replaceString is:"
echo -n "${replaceString}" | hexdump -C
fi
# What files to examine for string replacement
find "${inputDir}" -iname "*.[ch]" | while read file
do
if [ "${veryVerbose}" = "Yes" ]
then
echo
echo "file= ${file}"
fi
filename=$(basename "${file}")
if [ "${veryVerbose}" = "Yes" ]
then
echo "filename= ${filename}"
fi
echo -n "${file} "
rm -f "${outputDir}${filename}"
# Use perl for the string replacement. Allows lineend characters in string.
perl -e '
$verb = "'"${veryVerbose}"'";
open $logfile, "'"${file}"'";
$mydata = "";
while ($row = <$logfile>) { $mydata .= $row; }
close $logfile;
$originalMydata=$mydata;
if ( $verb eq "Yes" ) { print "...mydata is \n$mydata\n"; }
$input = "'"${findString}"'";
if ( $verb eq "Yes" ) { print "input = $input\n"; }
$output = "'"${replaceString}"'";
if ( $verb eq "Yes" ) { print "output = $output\n"; }
$mydata =~ s/\Q$input/$output/g;
if ( $verb eq "Yes" ) { print "--->mydata is changed to\n";
print "$mydata\n"; }
if ( $originalMydata eq $mydata ) {print "no change.\n";} else
{
open($fh, ">", "'"${outputDir}${filename}"'") or die "Could not open file $!";
print $fh "$mydata";
close $fh;
print "\nUpdated test file: '"${filename}"'\n\n";
}
if ( $verb eq "Yes" ) { print "perl complete.\n";} '
#cmp "$file" "/home/me/BitHoist/test/iout/${filename}"
done
exit