Geody Labs


# Main Index: Debian Linux Magic Spells Cheat Sheet (one liners, how to, tips and tricks)

# Text String Manipulation

Type a text string:
echo "STRING" # print STRING
echo -n "STRING" # print STRING without new line
echo {A,B,C}{A,B,C} # return all possible combinations of given characters (this example returns AA AB AC BA BB BC CA CB CC )

Type a text string (y by default) until interrupted:
yes # Type y until interrupted
yes n # Type n until interrupted
yes hello world | head --lines=5 # Type hello world 5 times
yes | COMMAND # automatically reply with "y" to all questions prompted by the COMMAND
yes n | COMMAND # automatically reply with "n" to all questions prompted by the COMMAND

Convert character encoding:
apt-get install recode
recode iso-8859-15..utf8 < FILE.TXT > file.utf8 # recode FILE.TXT from Latin Western Europe to UTF8
recode utf8..iso-8859-15 < FILE.UTF8 > file.txt # recode FILE.UTF8 from UTF8 to Latin Western Europe
recode ../b64 < FILE.TXT > file.b64 # recode FILE.TXT to Base 64
recode /qp.. < FILE.TXT > file.qp # recode FILE.TXT to quoted printable

Convert New Line Format:
sed -i "s/\r//" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # From \r\n (Windows) to \n (Unix/Linux/Mac)
sed -i "s/\n/\r\n/" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # From \n (Unix/Linux/Mac) to \r\n (Windows)

Strip trailing line break (new line) from output
echo "Test" | tr -d '\n' # In this example, this would be the same as echo -n "Test"

Convert tabs to spaces in a text file:
expand PATH/FILE  # convert only tabs at the beginning of the line
expand -a PATH/FILE  # convert all tabs, not just the ones at the beginning of the lines

Convert spaces to tabs in a text file:
unexpand PATH/FILE  # convert all tabs, not just the ones at the beginning of the lines
unexpand -i PATH/FILE # convert only spaces at the beginning of the lines

Search and Replace:
sed "s/SOURCE_STRING/REPLACEMENT/g" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace all occurrences of a string inside a text file, case sensitive
sed "s/SOURCE_STRING/REPLACEMENT/gi" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace all occurrences of a string inside a text file, case insensitive
sed "s/[Bb]ig/small/g" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace the word Big or big to small
sed "10,20 s/SOURCE_STRING/REPLACEMENT/gi" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace all occurrences of a string inside a text file, case insensitive, and process only lines 10 to 20 included
sed "/MATCH_STRING/ s/SOURCE_STRING/REPLACEMENT/g" PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace SOURCE_STRING with REPLACEMENT only in lines where MATCH_STRING is present
sed 's/TAG1.*TAG2/TAG1 REPLACEMENT TAG2/g' PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace anything between TAG1 and TAG2 keeping the tags, case sensitive
sed 's/TAG1.*TAG2/TAG1 REPLACEMENT TAG2/gi' PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace anything between TAG1 and TAG2 keeping the tags, case insensitive
sed 's/TAG1.*TAG2/REPLACEMENT/g' PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace anything between TAG1 and TAG2 removing the tags, case sensitive
sed 's/TAG1.*TAG2/REPLACEMENT/gi' PATH/SOURCE_FILE>PATH/DESTINATION_FILE # Replace anything between TAG1 and TAG2 removing the tags, case insensitive
find . \( -type d -name .git -prune \) -o -type f -name "*.txt" -print0 | xargs -0 sed -e 's/SOURCE_STRING/REPLACEMENT/g' # Replace all occurrences of SOURCE_STRING with REPLACEMENT (case sensitive) in all files which names ends with .txt inside the current directory and all its subdirectories except for the subdirectories called .git . This is a dry run ( -e flag for sed will print the result on standard output, without actually writing to the file )
find . \( -type d -name .git -prune \) -o -type f -name "*.txt" -print0 | xargs -0 sed -i 's/SOURCE_STRING/REPLACEMENT/g' # Replace all occurrences of SOURCE_STRING with REPLACEMENT (case sensitive) in all files which names ends with .txt inside the current directory and all its subdirectories except for the subdirectories called .git . This will actually replace the text in the files

Count the occurrences of a string within another string:
needle=","; haystack="one, two, three, four, five"; occurrences=$(grep -o "$needle" <<< "$haystack" | wc -l); echo $occurrences

Insert characters into a string:
echo '74657374' | sed 's/.\{2\}/&\./g' # Insert a dot every 2 characters and (it may leave a final dot)
echo '74657374' | sed 's/.\{2\}/&\./g' | awk '{sub(/\.$/, "")};1' # Insert a dot every 2 characters and remove the last dot if present (warning: it may remove a pre existing final dot)
echo '74657374' | sed 's/.\{4\}/& /g' # Insert a space every 4 characters and (it may leave a final space)
echo '74657374' | sed 's/.\{4\}/& /g' | awk '{sub(/ $/, "")};1' # Insert a space every 4 characters and remove the last space if present (warning: it may remove a pre existing final space)

Truncate a string:
echo $(cut -c 3 <<< "this_is_an_example") # Get the third character of a string. Example: before: "this_is_an_example", after: "h".
echo "this_is_an_example" | cut -c 3 # Get the third character of a string. Example: before: "this_is_an_example", after: "h".
echo "this_is_an_example" | cut -c 1 # Get the first character from a string. Example: before: "this_is_an_example", after: "t".
echo "this_is_an_example" | cut -c -3 # Get the first 3 characters from a string. Example: before: "this_is_an_example", after: "thi".
echo "this_is_an_example" | cut -c 4-9 # Get the characters ranging in position 4 up to 9 from a string. Example: before: "this_is_an_example", after: "s_is_a".
echo "this_is_an_example" | cut -c 3- # Cut the first 2 characters from a string (returns the string starting from the 3rd character). Example: before: "this_is_an_example", after: "is_is_an_example".

echo "this_is_an_example" | rev | cut -c 3 # Get the third character from the end of a string. Example: before: "this_is_an_example", after: "p".
echo "this_is_an_example" | rev | cut -c 1 # Get the last character of a string. Example: before: "this_is_an_example", after: "e".
echo "this_is_an_example" | rev | cut -c 1-3 | rev # Get the last 3 characters from the end of a string. Example: before: "this_is_an_example", after: "ple".
echo "this_is_an_example" | rev | cut -c 4-9 | rev # Get the characters ranging in position 4 up to 9 from the end of a string. Example: before: "this_is_an_example", after: "n_exam".
echo "this_is_an_example" | rev | cut -c 3- | rev # Cut the last 2 characters from a string (returns the string up to the 3rd character from the end). Example: before: "this_is_an_example", after: "this_is_an_examp".

Truncate a string using a marker:
# The marker (delimiter) to set the point where to truncate a string is ":" in the following examples
cut -d ":" -f1 <<< "this:is:an:example" # Truncate a string at the first occurrence of a marker (removing the marker). Example: before: "this:is:an:example", after: "this".
cut -d ":" -f2- <<< "this:is:an:example" # Truncate a string after the first occurrence of a marker (removing the marker). Example: before: "this:is:an:example", after: "is:an:example".
echo "this:is:an:example" | cut -d ":" -f1 # Truncate a string at the first occurrence of a marker (removing the marker). Example: before: "this:is:an:example", after: "this".
echo "this:is:an:example" | cut -d ":" -f2- # Truncate a string after the first occurrence of a marker (removing the marker). Example: before: "this:is:an:example", after: "is:an:example".
echo "this:is:an:example" | rev | cut -d ":" -f2- | rev # Truncate a string up to the last occurrence of a marker (removing the marker). Example: before: "this:is:an:example", after: "this:is:an".
echo "this:is:an:example" | rev | cut -d ":" -f1 | rev # Truncate a string after the last occurrence of a marker (removing the marker). Example: before: "this:is:an:example", after: "example".

Case conversion:
echo "Test"|tr '[:lower:]' '[:upper:]' # Return the text in UPPER case
echo "Test"|tr '[:upper:]' '[:lower:]' # Return the text in lower case
cat PATH/SOURCE_FILE|tr '[:lower:]' '[:upper:]'>PATH/DESTINATION_FILE # Convert a text file to Upper case
cat PATH/SOURCE_FILE|tr '[:upper:]' '[:lower:]'>PATH/DESTINATION_FILE # Convert a text file to lower case

Text Attributes

tput bold; echo "Bold Text"; tput sgr0; tput smul; echo "Underlined Text"; tput rmul; tput bold; tput smul; echo "Bold and Underlined Text"; tput rmul; tput sgr0; tput dim; echo "Dimmed (Half Bright) Text"; tput sgr0; tput blink; echo "Blinking Text"; tput sgr0; # Note: most terminals don't support blinking text and will show a reverse color text instead. tput rev; echo "Reverse Color Text"; tput sgr0; tput smso; echo "Standout Text"; tput rmso; # The visual output of a standout text depends on the terminal: generally is rendered as a reverse color text or a bold text. tput sgr0 # Turn off all attributes. txtbold=$(tput bold); txtnorm=$(tput sgr0); echo "Normal Text, ${txtbold}Bold Text${txtnorm}, Normal Text again."

Colored Text

# Note: Colors are not a POSIX (Portable Operating System Interface) feature in the Bash shell, so it might not be shown in some systems tput colors # Show the number of available colors in the terminal (generally 8) Typical ANSI colors in 8 color terminals: 0: Black 1: Red 2: Green 3: Yellow 4: Blue 5: Magenta 6: Cyan 7: White 8: Not used 9: Reset to default color tput setaf COLOR # Set foreground color (ANSI). tput setab COLOR # Set background color (ANSI). tput setab 1; tput setaf 3; echo -n "TEXT"; tput setaf 9; tput setab 9; echo; # Type TEXT in yellow over red # Escaped text colors echo -e "\e[1;37m WHITE \e[0;37m LIGHT GRAY \e[1;30m GRAY \e[0;30m BLACK \e[0;31m RED \e[1;31m LIGHT RED \e[0;32m GREEN \e[1;32m LIGHT GREEN \e[0;33m YELLOW \e[1;33m LIGHT YELLOW \e[0;34m BLUE \e[1;34m LIGHT BLUE \e[0;35m PURPLE \e[1;35m PINK \e[0;36m CYAN \e[1;36m LIGHT CYAN \e[0m DEFAULT"; Remove Escaped colors and other control characters from a text file: cat -v PATH/FILE | sed "s/\^\[\[.\;..m//g" | sed "s/\^\[\[.m//g" | sed "s/\^M$//g"




Please DONATE to support the development of Free and Open Source Software (PayPal, Credit Card, Bitcoin, Ether)

Page issued on 25-Sep-2022 04:11 GMT
Copyright (c) 2022 Geody - Legal notices: copyright, privacy policy, disclaimer