Bash shell - Text Processing

Cut

  • Use this command when you want to cut the string.
cut [options] file

#options
-c 문자위치 : 잘라낼 문자 위치 지정
-f 필드     : 잘라낼 필드 지정
-d 구분자   : 필드를 구분하는 구분자 지정. 디폴트->tab
-s         : 필드구분자를 포함할 수 없다면, 그 행은 처리 제외

Practise - Cut user input file

########PRACTICE-1########
#read user input file and print 3rd letter#
cut -c3 |cat /dev/stdin

########PRACTICE-2########
#read user input file and print 2nd and 7th letter#
cut -c2,7 |cat /dev/stdin

########PRACTICE-3########
#read user input file and print 2nd to 7th letters#
cut -c2-7 |cat /dev/stdin

########PRACTICE-4########
#read user input file and print to 4th letters#
cut -c-4 | cat /dev/stdin

########PRACTICE-5########
#read user input file and print to 4th letters#
cut -c-4 | cat /dev/stdin

########PRACTICE-6########
#read user input file and deleminate by tab#
cut -f1-3 -d$'\t' | cat /dev/stdin

########PRACTICE-6########
#read user input file and print nth letter to end#
cut -c13- | cat /dev/stdin

Additional #1 - Redirection and pipe

stdin : 표준입력(0, 키보드)
stdout : 표준출력(1, 모니터)
stderr : 에러출력(2, 모니터)

> : 출력 리다이렉션(stdout)
< : 입력 리다이렉션(stdin)
>> : 추가출력 리다이렉션(덮어쓰지 않고 새로 추가)
2> : 표준에러 리다이렉션(stderr)

|  : 하나의 출력을 파일이 아닌 다른 프로세스로 리다이렉트

-d option : 처리 시 single character 이상 자르기 위해서 -d$'\t' 와 같이 옵션 추가
  • Use this command when you want to cut the text file with nth line manupulation.
head [options] [file]

#options
-n #ofLine  : n번째 줄까지 출력
-c byte     : n바이트까지 출력

Practise - Head user input file

########PRACTICE-1########
#read user input file and print head to 20th lines#
head -n 20 | cat /dev/stdin
or
head -20 | cat /dev/stdin

########PRACTICE-2########
#read user input file and print 20 characters (20byte) #
head -c 20 | cat /dev/stdin

########PRACTICE-3########
#read user input file and print 12th to 22th lines
head -22 | tail -n +12  /dev/stdin

Tail

  • Use this command when you want to cut the text file with nth line manupulation.
tail [options] file

#options
-n number  : 마지막에서 n번째 줄까지 출력
-n +number : 처음에서 n번재 줄부터 마지막까지 출력
-c number  : 마지막에서 n 바이트 출력

Practise - Tail user input file

########PRACTICE-1########
#read user input file and print last nth lines
tail -n 20

########PRACTICE-2########
#read user input file and print 20 characters (20byte) #
tail -c 20

Tr

  • tr command stands for translate.
tr [options] set1 [set2]

#options
-d sequence : 규칙 삭제
-s sequnece : sequnce를 하나만 남기고 삭제(shrink)

Practise - tr user input file

########PRACTICE-1########
#read user input file and change () -> [] 
cat /dev/stdin | tr '()' '[]'

########PRACTICE-2########
#read user input file and delete lower case 
cat /dev/stdin | tr -d [:lower:]

########PRACTICE-3########
#read user input file and leave one space
cat /dev/stdin | tr -s [:space:]

Sort

  • sort contents of input file
sort [option ]input

#options
-r : reverse 
-n : numeric sort

Practise - sort user input file

########PRACTICE-1########
#read user input file and sort asc
sort /dev/stdin

########PRACTICE-2########
#read user input file and sort desc
sort -r /dev/stdin

########PRACTICE-3########
#read user input file and sort by number
sort -n /dev/stdin

########PRACTICE-3########
#read user input file and sort by number desc
sort -n -r /dev/stdin

results matching ""

    No results matching ""