【问题标题】:How can I fill the gaps and then continue this sequence?我怎样才能填补空白,然后继续这个序列?
【发布时间】:2019-05-21 21:59:04
【问题描述】:

我的代码查看序列中使用的最后一个活动设备,然后继续它。如果序列中存在当前未使用的空白,我想填补它。如何将其构建到代码中?

脚本按顺序按预期工作。我不确定从哪里开始添加功能来填补空白。

输入:

bash script WABEL8499IPM 3

脚本:

 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)

 # create 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

 # Fill In Sequence If Any Spots are Available If 101, 102, 104, 
 # 105, 106, 107 and 108 are used I want to output 103 (to fill in), 
 # 109 and 110 (to continue sequence).
 # 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

实际结果:

  #OUTPUT FROM WABEL8499IPM, 3
  #IF WABEL8499IPM101,102,104,105 ARE USED THEN OUTPUT IS THIS: 
  WABEL8499IPM106
  WABEL8499IPM107
  WABEL8499IPM108

期望/预期结果:

  #OUTPUT FROM WABEL8499IPM, 3
  #IF WABEL8499IPM101,102,104,105 ARE USED THEN OUTPUT IS THIS: 
  WABEL8499IPM103
  WABEL8499IPM106
  WABEL8499IPM107

基本上,在我当前的脚本中,我正在调用 API 以查看当前注册到 MDM 中的内容,然后查看序列中的最高数字并输出序列中的下一个数字。目标是在序列未完成的任何空白处填写序列。

【问题讨论】:

  • 你复制正确了吗? +=($(echo $device_name$cnum)) 不是一个有效的语句,它缺少开头的变量。
  • 我没有。现在应该修好了。
  • 如果序列是103,104怎么办?要输出101,102,105...吗?
  • 您向我们展示了实际输出和预期输出,但您没有向我们展示输入 - 将其添加到您的问题中,以便我们开始尝试帮助您。
  • @mickp 是的,完全正确。在示例中,我给出了 101,102,104 和 105 正在使用中,所以我想要 103,106 和 107。如果你问的情况正好相反,那么我希望输出先填充然后继续序列。

标签: bash awk sed grep sequence


【解决方案1】:

这可能对你有用:

# create a test source file

$ cat > src_file <<-EOF
foo WABEL8499IPM102 bar WABEL8499IPM108 foo bar
WABEL8499IPM106
foo WABEL8499IPM104
foo bar
WABEL8499IPM105 WAbel8499IPM110 bar
EOF
# the actual code

$ cat script
#!/usr/bin/env bash

pre=$1
num=$2
f=src_file
s=101

((num==0)) && exit

grep -oP "$pre\K[0-9]+" "$f" | sort -n > tmp 

comm -13 tmp <(seq $s $((s+num+$(wc -l < tmp)))) | awk -v n=$num -v p="$pre" '{print p $0}NR>=n{exit}'
# execute the script

$ ./script WABEL8499IPM 5
WABEL8499IPM101
WABEL8499IPM103
WABEL8499IPM107
WABEL8499IPM109
WABEL8499IPM110

【讨论】:

  • 我运气不太好,因为我不确定如何实现。
  • @joshuaspatrick 我包含了一个测试运行来演示如何使用代码。
猜你喜欢
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 2012-06-14
  • 2020-07-22
  • 1970-01-01
相关资源
最近更新 更多