Shell脚本自制字符替换version1.0~1.4

小白自学练习。

脚本命名:script1_0.bash

用途:用于单个字符替换,无法批量替换

#! /bin/bash -
#You can use this command for modifying words from files by using regular expression.
#Command language:	script1_0.bash old_words new_words file

umask 077 

IFS='
 	'
OLDPATH="$PATH"

PATH=/bin:/usr/bin
export PATH
old_word=$1 #获取输给命令的第一个参数
new_word=$2 #获取输给命令的第二个参数
file=$3 #获取输给命令的第三个参数
PROGRAM=`basename $0` #获取命令名 `...`这类似于引号的点的作用是相当于在shell端口的命令提示符上,也可以用$(...) 代替。
                      #输入shell上的脚本,这里用到了shell自带的basename脚本

#下列error() usage() usage_and_exit()均是借鉴shell脚本学习指南
error()
{
	echo $@ 1>&2  
	usage_and_exit 1
}

usage()
{
	echo "Usage: $PROGRAM Old_Word New_Word file_name"
}

usage_and_exit()
{
	usage
	exit $1

}

if test $# -ne 3  #测试命令行参数是否为三个,$#为命令行参数个数的变量
then
	error without enough argument! #这里使用了error()子函数,error后面是子函数的参数
else
	DS=`eval date | awk  '{print $4}' | awk -F: '{print $1$2$3}'` #DS=Date Suffix 
    #eval date用来获取当前日期时间
    #awk 以空格为分隔符,打印第4个字段,用于获取时分秒
    #awk -F: 是以冒号为分隔符,获取字段在打印,目的:删掉冒号分隔符。
    FN=`eval echo "$file"| awk -F. '{print $1}'` 
    #FN=File Name 去掉后缀去掉。还有basename也                
    #可以去掉后缀
	OFS=`eval echo "$file"| awk -F. '{print $2}'` #OFS=Original File Suffix
	new_file="$FN"_"$DS"."$OFS" #组成新的文件名
	echo Finished job!
	echo "The result was stored in $new_file"
fi
	
	

 
cp -r $file $new_file #复制文件
chmod -R 700 $new_file #更改文件权限
sed "s;\<"$old_word"\>;"$new_word";g" <$file >$new_file
#用sed进行正则表达式替换,\<...\>是单词定界符结构。

脚本命名:script1_1.bash

用途:用于单个字符替换,无法批量替换

与script1_0.bash的不同点:

1. v1.0主要利用awk来构建所需的新文件名。

2. v1.1利用sed正则表达式直接获所需的新文件命名。

#! /bin/bash -
#You can use this command for modifying words from files by using regular expression.
#Command language:	script1_1.bash old_words new_words file

umask 077

IFS='
 	'
OLDPATH="$PATH"

PATH=/bin:/usr/bin
export PATH
old_word=$1
new_word=$2
file=$3
PROGRAM=`basename $0`

error()
{
	echo $@ 1>&2
	usage_and_exit 1
}

usage()
{
	echo "Usage: $PROGRAM Old_Word New_Word file_name"
}

usage_and_exit()
{
	usage
	exit $1

}

if test $# -ne 3
then
	error Without enough argument! #这里使用了error()子函数,error后面是子函数的参数
else
	DS=`eval date | awk  '{print $4}' | awk -F: '{print $1$2$3}'` #DS=Date Suffix
	new_file=`echo $file | sed "s;\(^.*\)\.;\1_$DS\.;"`
	#这里的sed以分号作为分隔符,\(...\)独立的子模式结构,用\1来获取子模式所匹配的内容。
    echo Finished job!
	echo "The result was stored in $new_file"
fi
	
	

 
#cp -r $file $new_file
sed "s;\<"$old_word"\>;"$new_word";g" <$file >$new_file
chmod -R 700 $new_file

脚本命名:script1_2.bash

用途:用于批量替换字符串

#! /bin/bash -
#You can use this command for modifying words from files by using regular expression.
#Command language:	script1_2.bash words_file file

umask 077

IFS='
 	'
OLDPATH="$PATH"

PATH=/bin:/usr/bin
export PATH
word_file=$1
#word_file 必须包含两列内容,一列为被替换字符串,另一列为对应的替换字符串,即新的字符串
file=$2
arg_ned=2 #arg_ned=arg needed
PROGRAM=`basename $0`	#get sript name

error()
{
	echo $@ 1>&2
	usage_and_exit 1
}

usage()
{
	echo "Usage: $PROGRAM Words_file file_name"
}

usage_and_exit()
{
	usage
	exit $1

}

	
main()
{
	DS=`eval date | awk  '{print $4}' | awk -F: '{print $1$2$3}'` #DS=Date Suffix
	new_file=`echo $file | sed "s;\(^.*\)\.;\1_$DS\.;"`
	cp -r $file $new_file
	chmod -R 700 $new_file
	sleep 0.01 #睡眠0.01秒,提供一个复制时间
	while read old_word new_word #read 是shell的脚本,read上的变量名用于存取每行文件上的字 段,默认以空格为分隔符,old_word new_word为read新建的变量名。
	do
		echo "$old_word => $new_word " #用于提示所改的内容。
		sed "s;\<"$old_word"\>;"$new_word";g" < $new_file | tee >$new_file
         #tee用于获取管道输出的内容然后存储到new_file文件上,缺点:有时得到的新文件内容为空。
	done < $word_file 
	echo Finished job!
	echo "The result was stored in $new_file"
	exit 1
}


if test $# -ne $arg_ned
then
	error Without enough argument! #这里使用了error()子函数,error后面是子函数的参数
else
	main
fi

脚本命名:script1_3.bash

用途:用于批量替换字符串

与script1_2.bash的不同点:

1. v1.2主要利用tee获取输出内容。因为发现同时使用相同文件名读取和输出,即 <>$new_file 无法保存上一次所更改的内容。缺点:有时得到的新文件内容为空。

2. v1.3利用循环模式获取下一个保存所需内容的新文件名。

#! /bin/bash -
#You can use this command for modifying words from files by using regular expression.
#Command language:	script1 matching_file file

umask 077

IFS='
 	'
OLDPATH="$PATH"

PATH=/bin:/usr/bin
export PATH
word_file=$1
file=$2
arg_ned=2 #arg_ned=arg needed
PROGRAM=`basename $0`	#get sript itself's name
LN=`cat $word_file |tr -s '\n'| wc -l ` #LN=Line Number
#word_file获取实际包含字符串的行数,cat获取文件内容,tr用于删除文件里的空白行。
echo "$word_file's have $LN line"
error()
{
	echo $@ 1>&2
	usage_and_exit 1
}

usage()
{
	echo "Usage: $PROGRAM matching_file file_name"
}

usage_and_exit()
{
	usage
	exit $1

}

	
main()
{
	i=1
	DS=`eval date | awk  '{print $4}' | awk -F: '{print $1$2$3}'` #DS=Date Suffix
	new_file=`echo $file | sed "s;\(^.*\)\.\(.*\);\1_$DS\.\2$i;"`
	#获取第一个新文件名用于存储$file文件内容
    cp -r $file "$new_file"
	chmod -R 700 $new_file
#	sleep 0.01
	while read old_word new_word
	do
		if test $old_word #测试是否是有效字符,用于除去空白行,以便下面的i计算实际有效行数
		then
			echo "$old_word => $new_word "
			if test $i -ne $LN  #测试是否已经是word_file的最后一行.
			then
				xnew_file=`echo $file | sed "s;\(^.*\)\.\(.*\);\1_$DS\.\2$(($i+1));"`
				#获取下一个所需的新文件名名称,用$(($i+1))更新后缀数字从而获取新的文件名,用于保存每次更改内容。
                #注意此时的i的值并没有改变,不要想错了,只是+1而已,并没有新赋值给i。
                sed "s;\<"$old_word"\>;"$new_word";g" < "$new_file" >"$xnew_file"
				rm -rf $new_file    #删除已被读取内容的新文件
				new_file="$xnew_file"    #new_file更改为下次需要读取内容的文件名
				echo $xnew_file
				i=$(($i+1))    #用于计算已替换行数,并用于下一个新文件名的后缀计算。
			else
				FN_file=`echo $file | sed "s;\(^.*\)\.;\1_$DS\.;"` #FN=Final
				#如果已到word_file的最后一行,则将结果存储到最后的所需的文件名当中。
                sed "s;\<"$old_word"\>;"$new_word";g" < $new_file >$FN_file
				rm -rf $new_file
				chmod -R 700 $FN_file
			fi
		fi
	done < $word_file 
	echo Finished job!
	echo "The result was stored in $FN_file"
	exit 1
}


if test $# -ne $arg_ned
then
	error Without enough argument!
else
	main
fi

 脚本命名:script1_4.bash

用途:用于批量替换字符串

与script1_3.bash的不同点:

1. v1.3比v1.4要复杂些,v1.3的逻辑是将所需修改文件(file)的全部内容通过sed遍历words_file的第一行后就输出到新的文件,再用新的文件内容通过sed遍历words_file内容的下一行非空白行。

2. v1.4则与v1.3逻辑相反,且代码简单一些。在每次读取所需修改文件(file)的一行后通过sed遍历完words_file里的每一行,再通过>>操作符将改好的行(line)输出到新的文件下,>>操作符的作用是将输出内容附加到文件尾部而不会覆盖文件内容。但v1.4编译速度会比v1.3的明显要慢。words_file超过50行v1.4就会明显停顿1到3秒后才出结果。

#! /bin/bash -
#You can use this command for modifying words from files by using regular expression.
#Command language:	script1_4.bash words_file file

umask 077

IFS='
 	'
OLDPATH="$PATH"

PATH=/bin:/usr/bin
export PATH
word_file=$1
file=$2
arg_ned=2 #arg_ned=arg needed
PROGRAM=`basename $0`	#get sript name
LN=`cat $word_file |tr -s '\n'| wc -l ` #LN=Line Number
echo "$word_file's have $LN line"

error()
{
	echo $@ 1>&2
	usage_and_exit 1
}

usage()
{
	echo "Usage: $PROGRAM Words_file file_name"
}

usage_and_exit()
{
	usage
	exit $1

}

	
main()
{
	DS=`eval date | awk  '{print $4}' | awk -F: '{print $1$2$3}'` #DS=Date Suffix
	new_file=`echo $file | sed "s;\(^.*\)\.;\1_$DS\.;"`
#	cp -r $file $new_file
#	chmod -R 700 $new_file
#	sleep 0.01
	while read -r line #read读取一整行存到line变量中
	do
		i=0
		while read old_word new_word
		do
			if test $old_word
			then
				#echo "$old_word => $new_word "
				i=$(($i+1))
				line=`echo $line | sed "s;\<"$old_word"\>;"$new_word";g"`
				if test $i -eq $LN
				then
					echo $line >>$new_file #$line遍历完word_file实际行数($LN)后才输出到文件。
				fi
			fi
		done < $word_file
	done < $file 
	chmod -R 700 $new_file
	echo Finished job!
	echo "The result was stored in $new_file"
	exit 1
}


if test $# -ne $arg_ned
then
	error Without enough argument!
else
	main
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值