[LINUX] [Shell] Various patterns of string decomposition

Introduction

--Although it has few uses, it may be processed by decomposing a character string character by character. ――At that time, it is necessary to deal with the options and Japanese problems that can be used in each environment. --This time, we will introduce multiple patterns of the character string decomposition command and record the safe patterns.

environment

result

--The following is the content of the result.

#pattern 1:fold and grep(v)Use of
#* In the case of full-width characters, blank lines will be included, so delete them with grep.
echo "Good" | fold -1 | grep -v '^$'

#Pattern 2:Use of sed and tr
#* The last line will be added, so delete it with the last sed.
echo "Good" | sed 's!.!& !g' | tr ' ' '\n' | sed '$d'

#Pattern 3:Use of sed and xargs(Safe)
echo "Good" | sed 's/./& /g' | xargs -n1

#Pattern 4: grep(o)Use of
#* The grep o option cannot be used depending on the environment.(Because POSIX is not supported)
echo "Good" | grep -o .
#Output result of each pattern
G
o
o
d

Contents

pattern 1

--Mainly the following processing. --Use fold to limit the output width to one character. --Use grep's v option to remove unnecessary blank lines. -** * If it is only half-width, there is no problem with just fold. ** **

Pattern 2

--Mainly the following processing. --Add a space after each character with sed. --Replace with line feed added by tr. --Remove unnecessary line breaks added last with sed

Pattern 3

--Mainly the following processing. --Add a space after each character with sed. -Specify the maximum value of the input argument using the n option of xargs.

Pattern 4

--Mainly the following processing. --Use grep's o option to output each character that matches. -** * The o option may not be available depending on the environment. Your own environment is possible. ** **

Summary

--From the above, it is safe to use ** pattern 3 sed and xargs ** from the viewpoint of environment and full-width support. ――However, we will consider using ** pattern 4 ** because it is short-form and there is no unnecessary processing.

reference

Recommended Posts

[Shell] Various patterns of string decomposition
Various character string operations
Summary of string operations
Various processing of Python
Various Python built-in string operations
Various import methods of Mnist
About various encodings of Python 3