At occasions, you could must take away characters from a string. No matter the reason being, Linux offers you with numerous built-in, useful instruments that let you take away characters from a string in Bash. This text exhibits you find out how to use these instruments to take away characters from a string.
The article covers find out how to carry out the next:
- Take away character from string utilizing sed
- Take away character from string utilizing awk
- Take away character from string utilizing lower
- Take away character from string utilizing tr
The instructions proven on this article had been carried out in Ubuntu 20.04 Focal Fossa. The identical instructions can be carried out on different Linux distributions which have the above instruments accessible. We’ll use the default Terminal utility to run the instructions. You’ll be able to entry the Terminal utility utilizing the Ctrl+Alt+T keyboard shortcut.
Take away Characters from String Utilizing sed
Sed is a strong and useful utility used for enhancing streams of textual content. It’s a non-interactive textual content editor that lets you carry out primary textual content manipulations on enter streams. You may as well use sed to take away undesirable characters from strings.
For demonstration functions, we are going to use a pattern string after which pipe it to the sed command.
Take away Particular Character from String
Utilizing sed, you’ll be able to take away a particular character from a string. For instance, to take away “h” from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | sed ‘s/h//’
This may solely take away the primary prevalence of ‘h’ within the string.
To take away all occurrences of ‘h’ from the string, use the next command:
$ echo “howdy, how are you?” | sed ‘s/h//g’
The place g stands for international. It is going to take away all occurrences of ‘h’ within the string.
Take away First Character from String
To take away the primary character from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | sed ‘s/^.//’ file
The place (.) matches precisely a single character and (^) matches any character at first of the string.
Take away Final Character from String
To take away the final character from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | sed ‘s/.$//’
The place (.) matches precisely a single character and ($) matches any character on the finish of the string.
Take away First and Final Character from String
To take away the primary and final character from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | sed ‘s/^.//;s/.$//’
Take away Characters from String Utilizing awk
Awk is a strong scripting language used for sample matching, together with textual content processing. Awk lets you filter and remodel textual content in numerous methods. You may as well use awk to take away characters from strings.
For demonstration functions, we are going to use a pattern string after which pipe it to the awk command.
Take away First Character From a String
To take away the primary character from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | awk ‘{ print substr( $0, 2 ) }’
The place ($0) is the entire goal string and (2) is the character beginning place. The above command removes the primary character, ‘h,’ character quantity ‘1,’ and returns the goal string starting with the second character, ‘e.’
Take away First Two Characters from String
You may as well take away a particular variety of characters from the start of a string. For instance, to take away the primary two characters from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | awk ‘{ print substr ($0, 3 ) }’
The above command will take away the primary two characters, ‘he,’ or character numbers ‘1 and a couple of,’ and returns the goal string starting with character quantity ‘3,’ or ‘l.’
Take away Final Character from String
To take away the final character from “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | awk ”
The place size($0)-1 means deducting ‘1’ from the entire character size.
The above command will print the string starting with character quantity ‘1’ as much as size($0)-1 to strip off the final character.
There are ‘19’ characters (together with areas) within the above string. The command will work by printing all characters, beginning with character ‘1’ and as much as character ‘18,’ whereas eradicating the final character ‘19.’
Take away Final Two Characters from String
To take away the final two characters from “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | awk ”
The place size($0)-2 means deducting ‘2’ from the entire character size.
The above command will print the string, starting with character quantity ‘1’ and as much as character quantity ‘size($0)-2,’ to take away the final two characters within the string.
Take away Each First and Final Characters from String
To take away each the primary and the final characters from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | awk ”
The place size($0)-2 means deducting ‘2’ from the entire character size.
The above command will print the string, starting with character quantity ‘2’ as much as character quantity ‘size($0)-2,’ to take away the primary and final character.
Take away Character from String Utilizing lower
Minimize is a command-line instrument generally used to extract a portion of textual content from a string or file and print the consequence to an ordinary output. You may as well use this command for eradicating characters from a string.
For demonstration functions, we are going to use a pattern string after which pipe it to the lower command.
Take away First Character from String
To take away out the primary character from the string, “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | lower -c 2-
This command will print the string, starting with the second character, whereas eradicating the primary character.
Take away First 4 Characters from String
To take away the primary 4 characters from the string “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | lower -c 5-
This command will print the string, starting from the fifth character, whereas eradicating the primary 4 characters.
Print String Between 2nd and fifth Characters
To print the string “howdy, how are you?” between the second and fifth characters, the command can be:
$ echo “howdy, how are you?” | lower -c 2-5
This command will print the string, starting from the second character and as much as the fifth character, whereas eradicating the remaining starting and ending characters.
Take away Final Character from String
To take away the final character from the string “howdy, how are you?” use the lower command with rev, as follows:
$ echo “howdy, how are you?” | rev | lower -c2- | rev
This command works by first reversing the string, then slicing the primary character, and at last reversing it once more to provide the desired output.
Take away Final 4 Characters from String
To take away the final 4 characters from the road “howdy, how are you?” the command can be:
$ echo “howdy, how are you?” | rev | lower -c5- | rev
This command works by first reversing the string, then slicing the primary 4 characters, after which reversing it once more to provide the desired output.
Take away First and Final Characters from String
To take away the primary and final characters from the string “howdy, how are you?” use the lower command with rev, as follows:
$ echo “howdy world!” | lower -c2- | rev | lower -c2- |rev
This command works by slicing the primary character, then reversing the string and slicing its first character, after which reversing it once more to provide the desired output.
Take away Character from String Utilizing tr
The tr command (brief for translate) is used to translate, squeeze, and delete characters from a string. You may as well use tr to take away characters from a string.
For demonstration functions, we are going to use a pattern string after which pipe it to the tr command.
Take away All Occurrences of Character
Utilizing the tr command, you’ll be able to take away all occurrences of lowercase or uppercase characters out of your string. As an illustration, to take away all occurrences of the lowercase character ‘h’ from the string, the command can be:
$ echo “Good day, how are you?” | tr -d h
Equally, to take away all occurrences of the uppercase character ‘H’ from the string, the command can be:
$ echo “Good day, how are you?” | tr -d H
You may as well use interpreted sequences to take away lowercase or uppercase letters:
$ echo “Good day, how are you?”| tr -d [:upper:]
$ echo “HELLO, How are you?”| tr -d [:lower:]
Take away All Occurrences of Lowercase and Uppercase Characters
You may as well take away all occurrences of each lowercase and uppercase characters from a string. As an illustration, the next command will take away all occurrences of the character ‘h,’ each lowercase and uppercase.
$ echo “Good day, how are you?” | tr -d ‘hH’
Take away All Occurrences of Characters in a Particular Vary
To take away all occurrences of characters from a string within the particular vary ‘d-h,’ the command can be:
$ echo “howdy, how are you?” | tr -d ‘d-h’
This command will take away all characters within the vary ‘d-h’ (d,e,f,g,h) within the string.
Conclusion
In Linux, there’ll at all times be multiple strategy to accomplish a easy job. The identical is true with eradicating characters from a string. This text confirmed you 4 other ways to take action, together with a couple of examples for eradicating undesirable characters from a string. Deciding which instrument to make use of all relies on your preferences and, extra importantly, on what you wish to obtain.
tr remove whitespace,bash trim newline,bash trim string to length,bash trim quotes,bash no whitespace,remove space from line bash,bash extract substring regex,bash string length,bash substring after character,bash string replace regex,bash string split,bash remove substring,learn bash in,bash programming cheat sheet pdf,bash file extension,bash greg,bash faq greg's wiki,bash pipe,awk remove first and last characters,ksh replace last character in string,awk remove first character,bash remove special characters from string,sed remove first character from specific line,bash remove first n characters from each line,bash variable substitution in quotes,bash parameter expansion cheat sheet,bash nested parameter expansion,bash string substitution,bash variable expansion in string,parameter expansion tutorial,trim command in unix shell script,bash remove substring from middle of string,how to cut a string after a specific character in unix,bash remove character from string,bash remove last character from string,bash remove spaces from string,bash remove trailing whitespace from file,bash trim characters