gpt4 book ai didi

linux - 比较linux KSH脚本中的两个字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:26 25 4
gpt4 key购买 nike

我有两个文件:file1 和 file2。以下是文件内容的示例:

<TG>
<entry name="KEYNAME" val="" type="string" />
<entry name="KEYTYPE" val="" type="string" />
<entry name="TIMEZONE_OFFSET" val="-240" type="INT16" />
...
</TG>

我想检查file1中的某些行(包含字符串“entry name”的行)是否存在于file2中,如果存在,则比较这两行是否相同。

我实际上是通过复制 file1 创建了 file2,然后更改了一些值。问题是比较两个字符串变量不会返回正确的值。虽然在显示这两个变量时我可以看到它们是相同的,但比较的结果表明它们不是。我正在使用 Ksh。这是我的代码:

while read p; do
if [[ $p == *"entry name"* ]]; then
PARAM_NAME=$(echo $p | cut -d '"' -f2)
echo $PARAM_NAME
PARAM_OLD=$(grep $PARAM_NAME file2)
if [[ $PARAM_OLD == *"entry name"* ]]; then
echo $PARAM_OLD
echo $p
if [ "$PARAM_OLD" = "$p" ]; then
echo 'Identical values'
else
echo 'Different values'
fi
else
echo "$PARAM_NAME does not exist in previous version file. Using default value"
fi
fi
done <file1

我尝试了括号、等号和引号的所有可能性([]、[[]]、=、==、""、'""' 等)

这是我得到的输出:

<entry name="KEYNAME" val="" type="string" />
KEYNAME
<entry name="KEYNAME" val="" type="string" />
<entry name="KEYNAME" val="" type="string" />
Different values
<entry name="KEYTYPE" val="" type="string" />
KEYTYPE
<entry name="KEYTYPE" val="" type="string" />
<entry name="KEYTYPE" val="" type="string" />
Different values
<entry name="TIMEZONE_OFFSET" val="-24" type="INT16" />
TIMEZONE_OFFSET
<entry name="TIMEZONE_OFFSET" val="-240" type="INT16" />
<entry name="TIMEZONE_OFFSET" val="-24" type="INT16" />
Different values

我仍然发现字符串不同!我将不胜感激任何解释和帮助。

最佳答案

最新/较新的操作系统都支持 ksh 和 ksh93。

通过 ksh93,我们可以使用关联数组来限制我们对每个文件进行单次传递。

首先是一些示例数据:

$ cat file1
<TG>
<entry name="KEYNAME" val="" type="string" />
<entry name="KEYTYPE" val="" type="string" />
<entry name="KEYATTRIB" val="" type="string" />
<entry name="TIMEZONE_OFFSET" val="-241" type="INT16" />
</TG>

$ cat file2
<TG>
<entry name="KEYNAME" val="" type="string" />
<entry name="KEYTYPE" val="" type="stringX" />
<entry name="TIMEZONE_OFFSET" val="-240" type="INT16" />
</TG>

ksh93 脚本:

$ cat my_comp
#!/bin/ksh93

unset pline
typeset -A pline

# pull unique list of 'entry name' lines from file2 and store in our associative array pline[]:

egrep "entry name" file2 | sort -u | while read line
do
# strip out the 'entry name' value

x=${line#*\"}
pname=${x%%\"*}

# use the 'entry name' value as the index for our pline[] array

pline[${pname}]=${line}
done

# for each unique 'entry name' line in file1, see if we have a match in file2 (aka our pline[] array):

egrep "entry name" file1 | sort -u | while read line
do
# again, strip out the 'entry name' value

x=${line#*\"}
pname=${x%%\"*}

# if pname does not exist in file2

[ "${pline[${pname}]}" = '' ] && \
echo "\npname = '${pname}' : Does not exist in file2. Using default value:" && \
echo "file1: ${line}" && \
continue

# if pname exists in file2 but line is different

[ "${pline[${pname}]}" = "${line}" ] && \
echo "\npname = '${pname}' : Identical values for pname" && \
echo "file1: ${line}" && \
echo "file2: ${pline[${pname}]}" && \
continue

# if pname exists in file2 and line is the same

[ "${pline[${pname}]}" != "${line}" ] && \
echo "\npname = '${pname}' : Different values for pname" && \
echo "file1: ${line}" && \
echo "file2: ${pline[${pname}]}"
done

针对示例文件运行脚本:

$ my_comp

pname = 'KEYATTRIB' : Does not exist in file2. Using default value:
file1: <entry name="KEYATTRIB" val="" type="string" />

pname = 'KEYNAME' : Identical values for pname
file1: <entry name="KEYNAME" val="" type="string" />
file2: <entry name="KEYNAME" val="" type="string" />

pname = 'KEYTYPE' : Different values for pname
file1: <entry name="KEYTYPE" val="" type="string" />
file2: <entry name="KEYTYPE" val="" type="stringX" />

pname = 'TIMEZONE_OFFSET' : Different values for pname
file1: <entry name="TIMEZONE_OFFSET" val="-241" type="INT16" />
file2: <entry name="TIMEZONE_OFFSET" val="-240" type="INT16" />

回到普通的基本 ksh:

$ cat my_comp2
#!/bin/ksh

egrep "entry name" file1 | sort -u | while read line1
do
x=${line1#*\"}
pname=${x%%\"*}

# see if we can find a matching line in file2; need to strip off leading
# spaces in order to match with line1

unset line2
line2=$( egrep "entry name.*${pname}" file2 | sed 's/^ *//g' )

# if pname does not exist in file2

[ "${line2}" = '' ] && \
echo "\npname = '${pname}' : Does not exist in file2. Using default value:" && \
echo "file1: ${line1}" && \
continue

# if pname exists in file2 but lines are different

[ "${line2}" = "${line1}" ] && \
echo "\npname = '${pname}' : Identical values for pname" && \
echo "file1: ${line1}" && \
echo "file2: ${line2}" && \
continue

# if pname exists in file2 and lines are the same

[ "${line2}" != "${line1}" ] && \
echo "\npname = '${pname}' : Different values for pname" && \
echo "file1: ${line1}" && \
echo "file2: ${line2}"
done

针对示例文件运行脚本:

$ my_comp2

pname = 'KEYATTRIB' : Does not exist in file2. Using default value:
file1: <entry name="KEYATTRIB" val="" type="string" />

pname = 'KEYNAME' : Identical values for pname
file1: <entry name="KEYNAME" val="" type="string" />
file2: <entry name="KEYNAME" val="" type="string" />

pname = 'KEYTYPE' : Different values for pname
file1: <entry name="KEYTYPE" val="" type="string" />
file2: <entry name="KEYTYPE" val="" type="stringX" />

pname = 'TIMEZONE_OFFSET' : Different values for pname
file1: <entry name="TIMEZONE_OFFSET" val="-241" type="INT16" />
file2: <entry name="TIMEZONE_OFFSET" val="-240" type="INT16" />

关于linux - 比较linux KSH脚本中的两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45694193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com