Bash script 에서 case 는 아래와 같은 형태를 가진다.

case variable in

case1)

statement1;;

case2)

statement2;;

*)

statement_default;;

esac


'Programming > Bash Script' 카테고리의 다른 글

Bash script argument parsing with getopts  (0) 2020.01.23
Bash script for statement  (0) 2018.05.21
Bash Script Variable Split by Space  (0) 2018.02.04
Bash Script Echo  (0) 2018.02.04
Bash Script String  (0) 2017.07.04

Linux 사용시 파일 내부의 특정 표현을 수정하고자 하는 경우 sed 명령어를 활용할 수 있다.


sed 의 기본적인 형태는 아래와 같다.

sed <options> <address> <commands> <filename>

<options>

-n : suppress automatic printing (sed는 Matching 되는 Line 외에도 모든 Line 을 기본적으로 printing 하는데, line number 를 출력하고자 하는 경우(command =) 또는, Matching 되는 line 만 출력하고자 하는 경우(command /p)에는 -n option을 활용하면 된다.)

<address>

number : Match only the line number

/regular_expression/ : Match lines matching the regular_expression

addr1,addr2 : Match lines from addr1 to addr 2

addr,+N : Match addr and the following N lines

addr,~N : Match lines from addr to N*addr

<commands>

= : print the line number

/p : print the line

s/regular_expression/replacement : replace 


다만, 순전히 내 기준에서 가장 많이 사용하는 sed의 형태는 아래와 같다.

sed s/<regular_expression>/<replace>/g <file>

여기서 /g는 vim에서 replace 할 때의 /g 와 마찬가지로, 여러 개가 매칭 되는 경우 여러번 실행하도록 하는 옵션


sed 는 <file> 로 부터 <regular_expression> 을 찾아 <replace> 로 치환하여, 표준 출력을 해준다.

이는 vim 에서 replace 하는 것과 비슷하다.

결과물이 표준 출력 되기 때문에, 아래와 같이 > 또는 >> 를 이용해 파일에 써줄 수 있다.

다만, 여기서 파일이 이름이 같은 경우 아무것도 써지지 않는다는 점을 유의하자.

sed s/<regular_expression/<replace>/g <input_file> > <output_file>


아래는 예시

sed s/^$//g test.txt > test.txt.tmp

mv test.txt.tmp test.txt

(test.txt 파일로 부터 공백인 행을 없애 test.txt.tmp 파일에 출력한다.)


'Programming > Linux' 카테고리의 다른 글

Linux HDD mount  (0) 2018.04.06
HandBrakeCLI  (0) 2018.04.06
Linux find  (0) 2018.02.04
Ubuntu sources.list generation  (0) 2017.09.06
Ubuntu Redshift  (0) 2017.07.02

Bash Script 사용할 때,

shell 명령어에 대한 return을 받은 variable 이 space로 구분된 string 이라면,

아래와 같이 명령어를 배열처럼 구분하여 사용할 수 있다.

string_ex="I am your Father"

str_array=($string_ex)

echo ${str_array[0]} #I

echo ${str_array[1]} #am

echo ${str_array[2]} #you

echo ${str_array[3]} #Father


'Programming > Bash Script' 카테고리의 다른 글

Bash script for statement  (0) 2018.05.21
Bash Script case statement  (0) 2018.02.06
Bash Script Echo  (0) 2018.02.04
Bash Script String  (0) 2017.07.04
Bash Script If statement  (0) 2017.07.04

+ Recent posts