문자열의 정수를 한 줄에 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

관련 정보