본문 바로가기

3-1. Linux/::Command::

[BASH] 문자열

1.     패턴으로 문자열 자르기

1.1. 패턴에서 6번째  추출(/etc/passwd에서  디렉토리 추출)

# grep coriahn /etc/passwd | awk -F : '{print $6}'

Awk 이용해서 : 구분자로 6번째 필드를 출력


1.2. 패턴에서 마지막  추출(패스에서 마지막 패스 추출)

# echo ${PATH##*:}

: 구분자로 마지막값 출력 ## 의미는 일단 미지수


1.3. 패턴에서 마지막  잘라내기(파일명에서 확장자 제거)

# path=back.sh

# echo ${path%.*}

back

“.”을 구분자로 하여 마지막 패턴을 삭제하는듯


2.     추가사항은 나중에..

 

BASH: Split a string without ‘cut’ or ‘awk’

For a little test script I’m writing I needed to split a line on a ‘;’ but preservere the “s and ‘s, something that echo doesn’t like to do. Digging deeper into the bash docs I see that there are some handy string handling functions.

#!/bin/bash
line=’this “is” a command;this “is” a pattern’
COMMAND=${line%;*}
PATTERN=${line#*;}
echo $COMMAND
echo $PATTERN

And the output would be:

this “is” a command
this “is” a pattern


http://www.antonolsen.com/2006/04/10/bash-split-a-string-without-cut-or-awk/