전체
- 오지랖의 반대말 2018.03.05
- Bash Script case statement 2018.02.06
- Linux sed 2018.02.05
- Bash Script Variable Split by Space 2018.02.04
- Linux find 2018.02.04
- Bash Script Echo 2018.02.04
- 잠 2018.01.10
- 틀린 사람 2018.01.07
- 엉킴 2017.12.24
- Python 으로 탭/공백으로 구분 된 파일 parsing 하기 2017.12.19
오지랖의 반대말
Bash Script case statement
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
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 Variable Split by Space
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 |
Linux find
Linux system 에서 파일을 찾고자 할 떄,
find 함수를 사용하면 찾을 수 있다.
기본적인 사용법은 아래와 같다.
$ find <directory> -name <finding_file_name>
옵션으로 아래와 같이 사용 할 수 있다.
-type f : 파일
-type d : 폴더(directory)
-maxdepth 1 : find 는 기본적으로 하위 디렉토리까지 전부 검사 한다. maxdepth는 하위 몇 디렉토리 까지 검사할 것인지를 설정한다.
-and : 여러개의 조건에 대해 찾고자 할때 -and 옵션과 함께 조건을 추가할 수 있다.
-and ! : 해당 조건이 거짓인 경우에 대해 찾고자 할 때, -and ! 을 사용하면 된다.
-iname : 파일 이름의 대소문자를 고려하지 않고 검색한다.
아래는 예시
$ find . -type f -name 'abc*' -and ! -name 'abc'
(현재 디렉토리로 부터 abc로 시작하는 이름을 가진 파일 중에 파일 이름이 abc는 아닌 것을 찾는다.
xargs와 함께 사용하면,
디렉토리에서 원하는 파일을 복사/삭제 하기 수월하다
예시
$ find . -type f -name 'abc*' -and ! -name 'abc' | xargs rm -f
(현재 디렉토리로 부터 abc로 시작하는 이름을 가진 파일 중에 파일 이름이 abc는 아닌 것을 찾아 삭제한다.
'Programming > Linux' 카테고리의 다른 글
HandBrakeCLI (0) | 2018.04.06 |
---|---|
Linux sed (0) | 2018.02.05 |
Ubuntu sources.list generation (0) | 2017.09.06 |
Ubuntu Redshift (0) | 2017.07.02 |
Linux timezone (0) | 2017.06.20 |
Bash Script Echo
Bash Script echo 사용 시 줄바꿈을 하고 싶지 않다면,
-n
$ echo -n "hello "
$ echo "world"
hello world
'Programming > Bash Script' 카테고리의 다른 글
Bash Script case statement (0) | 2018.02.06 |
---|---|
Bash Script Variable Split by Space (0) | 2018.02.04 |
Bash Script String (0) | 2017.07.04 |
Bash Script If statement (0) | 2017.07.04 |
Bash script argument (0) | 2017.06.20 |
잠
틀린 사람
엉킴
Python 으로 탭/공백으로 구분 된 파일 parsing 하기
Python 으로 tab/공백 으로 구분 된 파일 parsing 하여 array로 만들기
#input file open
file = open(file_name, "r")
#output array of table
table_data = []
#for every row
for line in file:
#split by white spaces(column)
tmp_array = line.strip().split(" ")
#remove NO value element
while tmp_array.count(""):
tmp_array.remove("")
table_data.append(tmp_array)
'Programming > Python' 카테고리의 다른 글
Python Apt-get (1) | 2018.08.03 |
---|