![Bash 스크립트를 사용하여 변수 추가](https://linux55.com/image/62407/Bash%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%EB%B3%80%EC%88%98%20%EC%B6%94%EA%B0%80.png)
다음 텍스트가 포함된 파일이 있습니다.
//
// test_file.h
// test project
//
// Created by Test User
//
#ifndef test_file_constants_h
#define test_file_constants_h
// Generic includes
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define PF_MASTER_VERSION 3.0.0
#define PF_BUILD_VERSION 1
#endif
PF_BUILD_VERSION
실행할 때마다 1씩 증가하는 스크립트를 작성해야 합니다 . 보려고 했는데sed그리고AWK그리고 실패했어요!
답변1
다음을 기반으로 하는 솔루션 awk
:
awk '/^#define PF_BUILD_VERSION / {$3++} 1' infile >outfile && mv outfile infile
답변2
펄 방법:
perl -pe 's/^#define PF_BUILD_VERSION \K(\d+)/$1+2/e' file > newfile
또는 해당 위치에서 파일을 편집합니다.
perl -i -pe 's/^#define PF_BUILD_VERSION \K(\d+)/$1+2/e' file