linux : 剪切并比较两个文件名
linux : cut and compare two file names
我有以下情况
有两个文件 - sample.war & sample1.48.war
现在,我只需要剪切第一个文件的第一个字段。即 'sample'。 [我用简单的剪切命令做了这个]
接下来,我必须计算 'sample' 中的文本总数并仅剪切文件 2 (sample1.48.war) 中的那些字符
如果这两个字符匹配,则进行一些操作,如果不匹配,则进行其他操作。
有什么办法可以做到吗?
试试这个
#!/usr/bin/ksh
a=`echo sample.txt|sed 's/\.[^.]*$//'`
b=`echo sample1.48.txt|sed 's/\.[.^]*$//'`
echo $a $b
len_a=`echo $a|wc -c`
echo $len_a
cut_b=`expr $len_a - 1` // As the length of a will be 1 character extra due to the new-line so you have to cut b (length of a -1)
echo $cut_b
sub_b=`echo $b|cut -c1-$cut_b`
echo $sub_b
if [ $a = $sub_b ]
then
echo 'string matched' $a $sub_b
else
echo 'not matched' $a $sub_b
fi
输出:
sh-4.3$ test.sh
sample sample1.48.txt
string matched sample sample
我有以下情况
有两个文件 - sample.war & sample1.48.war 现在,我只需要剪切第一个文件的第一个字段。即 'sample'。 [我用简单的剪切命令做了这个] 接下来,我必须计算 'sample' 中的文本总数并仅剪切文件 2 (sample1.48.war) 中的那些字符 如果这两个字符匹配,则进行一些操作,如果不匹配,则进行其他操作。
有什么办法可以做到吗?
试试这个
#!/usr/bin/ksh
a=`echo sample.txt|sed 's/\.[^.]*$//'`
b=`echo sample1.48.txt|sed 's/\.[.^]*$//'`
echo $a $b
len_a=`echo $a|wc -c`
echo $len_a
cut_b=`expr $len_a - 1` // As the length of a will be 1 character extra due to the new-line so you have to cut b (length of a -1)
echo $cut_b
sub_b=`echo $b|cut -c1-$cut_b`
echo $sub_b
if [ $a = $sub_b ]
then
echo 'string matched' $a $sub_b
else
echo 'not matched' $a $sub_b
fi
输出:
sh-4.3$ test.sh
sample sample1.48.txt
string matched sample sample