특정 문자열을 바꾸는 방법은 무엇입니까?

특정 문자열을 바꾸는 방법은 무엇입니까?

내 텍스트 파일의 예:

03/Oct/2016:06:39:50-0500,cd/base/0/48/2.png,206,1514
03/Oct/2016:06:39:50-0500,cd/base/1/46/3.png,206,5796
03/Oct/2016:06:39:50-0500,cd/base/2/45/4.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/3/46/78.png,200,7208
03/Oct/2016:06:39:50-0500,cd/base/4/45/43.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/5/46/8.png,200,7208
...

base/이 텍스트에서는 다음 규칙을 따른 후 숫자를 바꿔야 합니다 .

if that_number=0 then that_number=5
if that_number=1 then that_number=6
if that_number=2 then that_number=7
if that_number=3 then that_number=8
if that_number=4 then that_number=9
if that_number=5 then that_number=10

원하는 결과:

03/Oct/2016:06:39:50-0500,cd/base/5/48/2.png,206,1514
03/Oct/2016:06:39:50-0500,cd/base/6/46/3.png,206,5796
03/Oct/2016:06:39:50-0500,cd/base/7/45/4.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/8/46/78.png,200,7208
03/Oct/2016:06:39:50-0500,cd/base/9/45/43.png,206,2252
03/Oct/2016:06:39:50-0500,cd/base/10/46/8.png,200,7208

어떻게 할 수 있는지 아시나요?

답변1

Perl에서는 둘 다 컨텍스트에 일치하는 것이 쉽습니다.그리고추가를 수행하십시오 :

perl -pe 's,base/\K\d+,$& + 5,e' input_file

형식의 모든 항목을 일치시키고 첫 번째 부분(최대 )은 잊어버리고 나머지 부분을 일치된 부분( )에 5를 더한 항목으로 바꿉니다 . 대체 항목을 단순한 문자열 대신 Perl 표현식으로 만듭니다.base/<numbers>\K$&e

답변2

awk+5각 숫자에 대한 작업 이라는 사실을 활용할 수 있습니다 .

awk -F'/' '{$5+=5 ; print}' OFS='/' input_file

/입력( -F'/') 및 출력( ) 에 대해 각각 필드 구분 기호로 사용하는 경우 OFS='/')필드 번호 5에 5를 추가해야 합니다.

여기서는 슬래시 위치와 숫자가 중요합니다.

답변3

sed작동 할 것이다:

sed \
    -e 's/base\/1/base\/6/' \
    -e 's/base\/2/base\/7/' \
    -e 's/base\/3/base\/8/' \
    -e 's/base\/4/base\/9/' \
    -e 's/base\/5/base\/10/' \
    -e 's/base\/0/base\/5/'

"base/0" 케이스를 마지막에 넣어야 한다고 생각합니다. 그렇지 않으면 5 -> 10 케이스도 시작됩니다.

관련 정보