我有一个包含以下信息的源文件。
WABEL8499IPM101
WABEL8499IPM102
WABEL8499IPM103
WABEL8499IPM104
WABEL8499IPM105
WABEL8499IPM106
WABEL8499IPM107
WABEL8499IPM108
我需要能够找到序列中最大的名称,然后使用序列中的下一个逻辑名称创建一个新变量。如果需要,我需要能够创建多个。例如:
使用grep在文件中搜索WABEL8499IPM,该文件显示了以上所有结果。我需要找到WABEL8499IPM108,因为它是序列中最大的,然后使用值WABEL8499IPM109创建一个新变量(多少取决于用户输入的值)。如果用户输入数量2,则我需要109和110。我的目标是构建一个bash脚本以输入基本名称(不包含最后3位数字),在序列中找到最大的名称,然后将其输出到日志文件中。用户需要多次使用顺序中的下一个名称。
我不太确定从哪里开始。我可以找到所有使用grep的人,但是很难找到最大的值/序列。用户将只输入基本名称,因为他们不知道最后3位数字。目前,我没有任何有效的代码。
SRCFILE="~/Desktop/deviceinfo.csv"
LOGDIR="~/Desktop/"
LOGFILE="$LOGDIR/DeviceNames.csv"
echo -e "\n"
echo "What is the base device name?"
read deviceName
echo "How many device names do you need?"
read quantityName
lines=$(grep -c "$deviceName" $SRCFILE)
echo -e "\n"
echo "There are $lines results."
deviceResults=$(grep -F "$deviceName" $SRCFILE)
echo -e "\n"
echo Device Name\'s Currently Enrolled:
echo "$deviceResults"
echo -e "\n"
echo "Your output file has been created."
CODE FOR CREATING OUTPUT FILE HERE
echo "$deviceName1" >> "$LOGFILE"
echo "$deviceName2" >> "$LOGFILE"
echo "$deviceName3" >> "$LOGFILE"
这种方法是否可以使用参考文件作为输入?例如,如果我必须研究和创建数量不同的多个名称,是否可以为此使用输入参考文件,这样就不必分别键入每个名称并多次运行脚本了?
答案 0 :(得分:1)
SRCFILE="~/Desktop/deviceinfo.csv"
LOGDIR="~/Desktop/"
LOGFILE="$LOGDIR/DeviceNames.csv"
# base name, such as "WABEL8499IPM"
device_name=$1
# quantity, such as "2"
quantityNum=$2
# the largest in sequence, such as "WABEL8499IPM108"
max_sequence_name=$(cat $SRCFILE | grep -o -e "$device_name[0-9]*" | sort --reverse | head -n 1)
# extract the last 3digit number (such as "108") from max_sequence_name
max_sequence_num=$(echo $max_sequence_name | rev | cut -c 1-3 | rev)
# creat a sequence of files starting from "WABEL8499IPM101" if there is not any "WABEL8499IPM".
if [ -z "$max_sequence_name" ];
then
max_sequence_name=device_name
max_sequence_num=100
fi
# create new sequence_name
# such as ["WABEL8499IPM109", "WABEL8499IPM110"]
array_new_sequence_name=()
for i in $(seq 1 $quantityNum);
do
cnum=$((max_sequence_num + i))
array_new_sequence_name+=($(echo $device_name$cnum))
done
#CODE FOR CREATING OUTPUT FILE HERE
#for fn in ${array_new_sequence_name[@]}; do touch $fn; done;
# write log
for sqn in ${array_new_sequence_name[@]};
do
echo $sqn >> $LOGFILE
done
用法:
bash test.sh WABEL8499IPM 2
日志文件中的结果:
WABEL8499IPM109
WABEL8499IPM110
已编辑
输入参考文件(input.txt):
WABEL8499IPM,2
WABEL8555IPM,6
WABEL8444IPM,5
驱动程序外壳脚本:
INPFIL="./input.txt"
PSRC="./test.sh"
cat $INPFIL | while read line;
do
device_name=`echo $line | cut -d "," -f 1`
quantity_num=`echo $line | cut -d "," -f 2`
bash $PSRC $device_name $quantity_num
done;
答案 1 :(得分:0)
您可以尝试
logdir="~/Desktop/"
srcfile="$logdir/deviceinfo.csv"
logfile="$logdir/DeviceNames.csv"
echo
read -p "What is the base device name? " deviceName
echo
read -p "How many device names do you need? " quantityName
echo
awk -v name="$deviceName" \
-v q="$quantityName" \
-v lelog="$logfile" '
$0 ~ "^"name {
sub(name,"")
a=a>$0?a:$0
}
END {
if ( a )
for ( i = 1 ; i <= q ; i++ )
print name ( a + i ) >> lelog
}
' "$srcfile"