text-processing
reference:
charset
[!NOTE|label:references:]
0x00
null
NUL
\0
^@
0x07
bell
BEL
\a
^G
0x08
backspace
BS
\b
^H
0x09
horizontal tab
HT
^I
0x0a
line feed
LF
^J
0x0b
vertical tab
VT
\v
^K
0x0c
form feed
FF
\f
^L
0x0d
carriage return
CR
^M
0x1a
Control-Z
SUB
-
^Z
0x1b
escape
ESC
\e
^[
list all charset
echo ascii in bash
escape
[!NOTE|label:references:]
\'
single quote
\"
double quote
\\
backslash
new line
horizontal tab
carriage return
\?
question mark
single quota & double quotas
[!TIP|label:references:]
3.1.2.5 Locale-Specific Translation Prefixing a double-quoted string with a dollar sign (
$), such as$"hello, world", will cause the string to be translated according to the current locale
control character


\001
SOH (Start Of Heading)
(0x01)
start of non-printing section
(== \[ in PS1)
\001\033[31m\002
Wrap ANSI color codes to avoid cursor position miscalculation
\002
STX (Start Of Text)
(0x02)
end of non-printing section
(== \] in PS1)
\001\033[0m\002
Wrap ANSI color codes to avoid cursor position miscalculation
\e
\033
ESC (Escape, 0x1B)
(0x1B)
begins an ANSI escape sequence
\e[31m (set text red)
Control text color, cursor position, terminal behavior
\a
BEL (BELL)
(0x07)
triggers terminal bell sound
echo -e "\a"
Alert user with sound
CR (Carriage Return)
(0x0D)
returns cursor to beginning of line
printf "Progress: 50%%\r"
Overwrite text on same line ( i.e. spinner/progress bar )
LF (line feed)
(0x0A)
newline
echo -e "Line1\nLine2"
Standard line break
TAB (Horizontal Tab)
(0x09)
horizontal tab
echo -e "Name:\tJohn"
Column-style output, Align text
\b
BS (Delete)
(0x08)
deletes one character backward
echo -e "abc\b\bxyz" (result: axyz)
Inline deletions
\xHH
Hex Byte
represents a character by its hex value
\x1B == \e
\x41 → A
Injecting arbitrary bytes
\uXXXX
Unicode Char
4-digit Unicode character (Bash extension, requires terminal support)
\u2714 → ✔️
Emojis or symbols in terminal
\nnn
Octal Char
3-digit octal character
\101 → A
Injecting arbitrary bytes
\033[XXm
SGR (Select Graphic Rendition)
text color/style control (i.e.: 31m=red,1m=bold,0m=reset)
\033[32;1mSuccess!\033[0m
Highlight key information
\033[K
-
erase to end of line
echo -e "Loading...\033[K"
Clear the content at the end of each line dynamically
\033[nA
-
cursor up n lines
\033[2A
Multi-line Output Control
\033[nB
-
cursor down n lines
\033[3B
Multi-line Output Control
\033[nC
-
cursor forward n columns
\033[10C
Align complex output
\033[nD
-
cursor backward n columns
\033[5D
Rollback Edit
encryption
base64
base64decryption
show
align
[!NOTE|label:see also]
numfmt
[!NOTE|label:references:]
setup
usage
convert format
padding
field
[!NOTE|label:references:]
combinations
single line to multiple lines
[!TIP]
xargs -n<x>tr
execute commands from file
[!TIP]
precondition
combine every 2 lines
[!NOTE|label:references:]
sample output
also using for sed output :
xargs
paste
sed
awk
while
combine every 3 lines
paste
awk
xargs
format output
[!TIP|label:sample data]
echo
[!TIP]

print file with ansicolor

echo -ne file with ansicolor
diff
[!NOTE|label:references:]
show all status
show diff only
show diff with
--<GTYPE>-group-formatVARIABLEAPPLICABLEoldGTYPE,LTYPEnewGTYPE,LTYPEunchangedGTYPE,LTYPEchangedGTYPE[!NOTE|label:tips:]
[G]TYPE:[g]roup:--<GTYPE>-group-format
[L]TYPE:[l]ine:--<LTYPE>-line-format
show common
create patch
[!NOTE|label:references:]
comm
diff
common
join
[!NOTE|label:references:]
alignment
[!TIP]
column (BSD)
[!NOTE|label:references:]
with header
sort
[!NOTE|label:references:]
sort the last column
awk:
print( $NF" "$0 ) | sort | cut -f2- -d' 'awk: similar with rev for words
get lines
get second-to-last line
[!NOTE|label:references:]
sed
tail & head
get next line by the pattern
awk
or
or
or get second column of next line of pattern
sed
to get docker registry mirrors
change next line of pattern
[!NOTE|label:references:]
replace
dollarsto$right after line of/Quarter [1-4]/sed
awk
replace
dollarsto$every 3 lines after/Quarter [1-4]/
get lines between 2 patterns
[!NOTE|label:reference:]
[!TIP] sample data:
sed
[!NOTE|label:references:]
include all patterns
exclude both patterns
exclude single pattern
with empty line
[!NOTE]
use case :
get line from pattern to the end
[!TIP|label:references:]
sample content:
get from first empty line to the end
[!NOTE|label:references:]
including pattern
[!TIP]
solution: to print from pattern to end
,$==,$pfor both CRLF and LF
sed
[!TIP]
solution: using line number to end:
n,$->"n"',$p'head -n1: for first matches pattern line numbertail -n1: for the last matches pattern line number
awk
not including pattern
[!TIP]
solution: to delete / not print from first line to pattern
delete:
/dnot print: -n
/!p
for both CRLF and LF
[!TIP]
solution: with matched line number + 1 :
"$(( n+1 ))"',$p'head -n1: for first matches pattern line numbertail -n1: for the last matches pattern line number
get from last empty line ( ^$ ) to end
^$ ) to end[!NOTE|label:references:]
awk
tac + awk
reverse search empty line
[!NOTE|label:for show TODO]
return first matching pattern
{% hint style='tip' %}
references:
[!TIP]
sed
awk
return second matching pattern search range
{% hint style='tip' %}
references:
Copying second occurrence pattern to a new file in sed {% endhint %}
[!TIP]
return the last matching pattern search range
[!NOTE|label:references:]
sed
awk
replace the last matching pattern
xargs
xargs{% hint style='tip' %}
references:
tips:
--delimiter=delim,-d delim
-I{}==-i
-n max-args,--max-args=max-args
-t,--verbose{% endhint %}
complex commands with xargs
[!NOTE|label:references:]
or using
$@
sort all shell script by line number
[!TIP] Pipe
xargsintofind
diff every git commit against its parent
[!TIP] precondition:
or
compress sub-folders
ping multiple IPs
[!TIP]
or
find
find[!NOTE|label:reference:]
output format
[!TIP|label:man find:]
%P File's name with the name of the command line argument under which it was found removed.
%f File's name with any leading directories removed (only the last element).
output file name only
cat config file in all .git folder
.git folderxargs&&catfind&&-exec
exec and sed
exec and sedchange IP address in batch processing
find and rename
find && tar
find && tar[!TIP] more can be found in imarslo: find and tar
backup all
config.xmlin JENKINS_HOMEback build history
find by timestamp
[!NOTE|label:references:]
via mtime
mtime[!TIP|label:tricky on
-mtime:]
Understanding find with atime, ctime, and mtime
+n: for greater than n
-n: for less than n
n: for exactly ni.e.:
-mtime -14: less than 14 days, not including 14 days == 13 days ago and less
-mtime +14: more than 14 days, not including 14 days == 15 days ago and more
via newermt
newermt[!TIP|label:tips for
-newerXY]
inject commands inside find
[!NOTE|label:references:]
printf
[!NOTE|label:references:]
formats
[!NOTE|label:references:]
time format
FORMATDESCRIPTIONEXAMPLE%Alast access time
%A+:2023-02-20+05:19:18.0000000000%Tlast modification time
%T@:1676899158.0000000000%tlast modification time in ctime format
Mon Feb 20 05:19:18.0000000000 2023%Clast status change time
%C+:2024-11-20+03:30:18.8905999140%clast status change time in ctime format
Wed Nov 20 03:30:18.8905999140 2024%Bbirth time
%B@:1676899158.0000000000TIME FIELDDESCRIPTIONEXAMPLE+date time
2023-02-20+05:19:18.0000000000@unix epoch
1676899158.0000000000Hhour
00..23k/Ihour in 24-hour / 12-hour format
00..23/01..12Mminute
00..59Ssecond
00..60pAM/PM
AM/PMT/Xtime in 24-hour format
hh:mm:ss:xxxxxxxxxxZtimezone
PST/PDTDATA FIELDDESCRIPTIONEXAMPLEa/Aabbreviated / full weekday
Wed/Wednesdayb(h) /Babbreviated / full month name
Jan/Januarymmonth
01..12dday of month
01..31wday of week
01->Monday;02->Tuesdayjday of year
001..366U/Wweek number: Sunday / Monday as first day
00..53y/Ylast 2-digits-of-year / 4-digits-of-year
00..99/1970..rtime in 12-hour format
hh:mm:ss [A/P]MFfull date; same as
%Y-%m-%d2023-02-20Ddate; same as
%m/%d/%y02/20/23xlocale date
02/20/2023name format
FORMATDESCRIPTIONEXAMPLE%pfile's name
./sample.txt%Pfile's name without starting-point
sample.txt%fbasename
sample.txt%hleading directories of file's name
.permision format
FORMATDESCRIPTIONEXAMPLE%mfile's mode
644%Mfile's mode in human-readable format
-rw-r--r--T: time in 24-hour format :hh:mm:ss.xxxxxxxxxxX: locale time :hh:mm:ss.xxxxxxxxxxc: locale time in ctime formatD: date :mm/dd/yyF: date :yyyy-mm-ddx: locale date :mm/dd/yyR: hour and minute in 24 hour format :HH:MM+: date and timecenter-align
left-align
mixed align
tips
[!TIP|label:rules:] size first, then md5 hash
trim
trim tailing chars
awk+rev${var:: -x})
remove all spaces
function in pip
remove empty lines
[!NOTE|label:references:]
remove empty line at the end of file
[!NOTE|label:references:]
remove duplicate empty lines
[!NOTE|label:references:]
[!NOTE|label:reference]
sample code:
${variable//search/replace}sed
echo "${string:0:$(( position - 1 ))}${replacement}${string:position}"
or
$ sed 's:\s\s*:|:g' <<< "${str}" aa|bb|cc
or
check line ending
[!NOTE|label:references:]
check ascii via terminal
$ man ascii
$ cat /usr/share/misc/ascii
$ printf "%d" "'${char}", i.e.:$ printf "%d" "'a" => 97
UNIX or Unix-like
ASCII
LF
0A
10
MS-DOS
ASCII
CR LF
0D 0A
13 10
\r
Commodore 8-bit machines
ASCII
CR
0D
13
QNX pre-POSIX
ASCII
RS
1E
30
\036
Acorn BBC and RISC OS
ASCII
LF CR
0A 0D
10 13
\n
Atari 8-bit machines
ATASCII
-
9B
155
-
IBM mainframe systems
EBCDIC
NL
15
21
\025
ZX80 and ZX81
non-ASCII
NEWLINE
76
118
-
od -chexdump -chexdump -Cvim
remove the ending '\n'
[!NOTE|label:references:]
original file
add '\n' to line-ending
[!TIP]
for ssh private key issue:
check last char in the file
add new line
add new line ending without modifying the file
fold
check the params valid
{% hint style='tip' %}
available params should be contained by 'iwfabcem' {% endhint %}
insert
insert new line
insert right after the second match string
{% codetabs name="original", type="bash" -%} DCR DCR DCR {%- language name="expected", type="bash" -%} DCR DCR check DCR {%- endcodetabs %}
insert after the matched string
with
gsubORIGINALAFTER INSERTwith variable
[!NOTE|label:references:]
ORIGINALAFTER INSERTINSERT TWO LINESwith sed
insert new line base on pattern
[!NOTE|label:references:]
awk
sed
write a file without indent space
or
{% codetabs name="example", type="bash" -%} $ sed -e 's:^\s*::' <<-'EOF' items.find ({ "repo": "${product}-${stg}-local", "type" : "folder" , "depth" : "1", "created" : { "${opt}": "4mo" } }) EOF items.find ({ "repo": "${product}-${stg}-local", "type" : "folder" , "depth" : "1", "created" : { "${opt}": "4mo" } }) {%- endcodetabs %}
cat
<< - and <<
<< - and <<This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.
The format of here-documents is:
bash
cat with specific character
$ cat -A sample.sh LANG=C tr a-z A-Z <<- END_TEXT$ Here doc with <<$ A single space character (i.e. 0x20 ) is at the beginning of this line$ ^IThis line begins with a single TAB character i.e 0x09 as does the next line$ ^IEND_TEXT$ $ echo The intended end was before this line$
$ bash sample.sh HERE DOC WITH <<- A SINGLE SPACE CHARACTER (I.E. 0X20 ) IS AT THE BEGINNING OF THIS LINE THIS LINE BEGINS WITH A SINGLE TAB CHARACTER I.E 0X09 AS DOES THE NEXT LINE The intended end was before this line
$ cat -A sample.sh LANG=C tr a-z A-Z << END_TEXT$ Here doc with <<$ A single space character (i.e. 0x20 ) is at the beginning of this line$ ^IThis line begins with a single TAB character i.e 0x09 as does the next line$ ^IEND_TEXT$ $ echo The intended end was before this line$
$ bash sample.sh sample.sh: line 7: warning: here-document at line 1 delimited by end-of-file (wanted `END_TEXT') HERE DOC WITH << A SINGLE SPACE CHARACTER (I.E. 0X20 ) IS AT THE BEGINNING OF THIS LINE THIS LINE BEGINS WITH A SINGLE TAB CHARACTER I.E 0X09 AS DOES THE NEXT LINE END_TEXT
ECHO THE INTENDED END WAS BEFORE THIS LINE
Last updated
Was this helpful?