每行将字符串中的整数增加 1

每行将字符串中的整数增加 1

例如 sed、awk 或 python。

原始txt文件中有548行

由此:

    http://twit.cachefly.net/audio/sn/sn0001/sn0001.mp3
    http://twit.cachefly.net/audio/sn/sn0001/sn0001.mp3
    http://twit.cachefly.net/audio/sn/sn0001/sn0001.mp3

对此:

    http://twit.cachefly.net/audio/sn/sn0001/sn0001.mp3
    http://twit.cachefly.net/audio/sn/sn0002/sn0002.mp3
    http://twit.cachefly.net/audio/sn/sn0003/sn0003.mp3

答案1

假设最后两个字段(在示例代码片段中)中使用的前缀sn是从倒数第三个字段派生的,这应该可以工作:

awk -vOFS='/' -F'/' '{$(NF-1)=sprintf("%s%04d", $(NF-2), FNR);
      $NF=$(NF-1)".mp3"; print}' file
http://twit.cachefly.net/audio/sn/sn0001/sn0001.mp3
http://twit.cachefly.net/audio/sn/sn0002/sn0002.mp3
http://twit.cachefly.net/audio/sn/sn0003/sn0003.mp3
http://twit.cachefly.net/audio/sn/sn0004/sn0004.mp3
http://twit.cachefly.net/audio/sn/sn0005/sn0005.mp3
http://twit.cachefly.net/audio/sn/sn0006/sn0006.mp3
http://twit.cachefly.net/audio/sn/sn0007/sn0007.mp3
http://twit.cachefly.net/audio/sn/sn0008/sn0008.mp3
http://twit.cachefly.net/audio/sn/sn0009/sn0009.mp3
http://twit.cachefly.net/audio/sn/sn0010/sn0010.mp3
http://twit.cachefly.net/audio/sn/sn0011/sn0011.mp3
http://twit.cachefly.net/audio/sn/sn0012/sn0012.mp3
http://twit.cachefly.net/audio/sn/sn0013/sn0013.mp3
http://twit.cachefly.net/audio/sn/sn0014/sn0014.mp3
http://twit.cachefly.net/audio/sn/sn0015/sn0015.mp3
http://twit.cachefly.net/audio/sn/sn0016/sn0016.mp3
http://twit.cachefly.net/audio/sn/sn0017/sn0017.mp3
http://twit.cachefly.net/audio/sn/sn0018/sn0018.mp3
http://twit.cachefly.net/audio/sn/sn0019/sn0019.mp3
http://twit.cachefly.net/audio/sn/sn0020/sn0020.mp3
http://twit.cachefly.net/audio/sn/sn0021/sn0021.mp3
http://twit.cachefly.net/audio/sn/sn0022/sn0022.mp3
http://twit.cachefly.net/audio/sn/sn0023/sn0023.mp3
http://twit.cachefly.net/audio/sn/sn0024/sn0024.mp3
http://twit.cachefly.net/audio/sn/sn0025/sn0025.mp3
http://twit.cachefly.net/audio/sn/sn0026/sn0026.mp3
http://twit.cachefly.net/audio/sn/sn0027/sn0027.mp3
http://twit.cachefly.net/audio/sn/sn0028/sn0028.mp3
http://twit.cachefly.net/audio/sn/sn0029/sn0029.mp3
http://twit.cachefly.net/audio/sn/sn0030/sn0030.mp3
http://twit.cachefly.net/audio/sn/sn0031/sn0031.mp3
http://twit.cachefly.net/audio/sn/sn0032/sn0032.mp3
http://twit.cachefly.net/audio/sn/sn0033/sn0033.mp3
http://twit.cachefly.net/audio/sn/sn0034/sn0034.mp3
http://twit.cachefly.net/audio/sn/sn0035/sn0035.mp3
http://twit.cachefly.net/audio/sn/sn0036/sn0036.mp3
http://twit.cachefly.net/audio/sn/sn0037/sn0037.mp3

答案2

#!/bin/bash
echo "Enter File you want to read"
read file
i=0;

if [ -f $file ]
then
      echo "file is avalible"
      while  read -r  line
      do
              echo "$line" | sed "s/http:\/\/twit.cachefly.net\/audio\/sn\/sn0001\/sn0001.mp3/http:\/\/twit.cachefly.net\/audio\/    sn\/sn000$i\/sn000$i.mp3/g"

      ((i++))

      done<$file





else
      echo "file is not abalible"
fi

在此输入图像描述

答案3

假设序列从 1 继续到 548,重新生成文件会更容易:

$ cat regen.sh
#!/bin/bash

for i in $(seq -w 1 548); do
     echo "http://twit.cachefly.net/audio/sn/sn0${i}/sn0{$i}.mp3"
done

$ bash regen.sh > newfile

$ cat netFile
http://twit.cachefly.net/audio/sn/sn0001/sn0001.mp3
http://twit.cachefly.net/audio/sn/sn0002/sn0002.mp3
http://twit.cachefly.net/audio/sn/sn0003/sn0003.mp3
http://twit.cachefly.net/audio/sn/sn0004/sn0004.mp3
...
http://twit.cachefly.net/audio/sn/sn0548/sn0548.mp3

相关内容