Linux remove last symbol

Removing Characters from String in Bash

At times, you may need to remove characters from a string. Whatever the reason is, Linux provides you with various built-in, handy tools that allow you to remove characters from a string in Bash. This article shows you how to use those tools to remove characters from a string.

The article covers how to perform the following:

  • Remove character from string using sed
  • Remove character from string using awk
  • Remove character from string using cut
  • Remove character from string using tr

The commands shown in this article were performed in Ubuntu 20.04 Focal Fossa. The same commands can also be performed on other Linux distributions that have the above tools available. We will use the default Terminal application to run the commands. You can access the Terminal application using the Ctrl+Alt+T keyboard shortcut.

Remove Characters from String Using sed

Sed is a powerful and handy utility used for editing streams of text. It is a non-interactive text editor that allows you to perform basic text manipulations on input streams. You can also use sed to remove unwanted characters from strings.

For demonstration purposes, we will use a sample string and then pipe it to the sed command.

Remove Specific Character from String

Using sed, you can remove a specific character from a string. For example, to remove “h” from the string “hello, how are you?” the command would be:

This will only remove the first occurrence of ‘h’ in the string.

To remove all occurrences of ‘h’ from the string, use the following command:

Where g stands for global. It will remove all occurrences of ‘h’ in the string.

Remove First Character from String

To remove the first character from the string “hello, how are you?” the command would be:

Where (.) matches exactly a single character and (^) matches any character at the beginning of the string.

Remove Last Character from String

To remove the last character from the string “hello, how are you?” the command would be:

Where (.) matches exactly a single character and ($) matches any character at the end of the string.

Remove First and Last Character from String

To remove the first and last character from the string “hello, how are you?” the command would be:

Remove Characters from String Using awk

Awk is a powerful scripting language used for pattern matching, along with text processing. Awk allows you to filter and transform text in various ways. You can also use awk to remove characters from strings.

For demonstration purposes, we will use a sample string and then pipe it to the awk command.

Remove First Character From a String

To remove the first character from the string “hello, how are you?” the command would be:

Where ($0) is the whole target string and (2) is the character starting position. The above command removes the first character, ‘h,’ character number ‘1,’ and returns the target string beginning with the second character, ‘e.’

Remove First Two Characters from String

You can also remove a specific number of characters from the beginning of a string. For example, to remove the first two characters from the string “hello, how are you?” the command would be:

Читайте также:  Компонент социальные сети для joomla

The above command will remove the first two characters, ‘he,’ or character numbers ‘1 and 2,’ and returns the target string beginning with character number ‘3,’ or ‘l.’

Remove Last Character from String

To remove the last character from “hello, how are you?” the command would be:

Where length($0)-1 means deducting ‘1’ from the total character length.

The above command will print the string beginning with character number ‘1’ up to length($0)-1 to strip off the last character.

There are ‘19’ characters (including spaces) in the above string. The command will work by printing all characters, starting with character ‘1’ and up to character ‘18,’ while removing the last character ‘19.’

Remove Last Two Characters from String

To remove the last two characters from “hello, how are you?” the command would be:

Where length($0)-2 means deducting ‘2’ from the total character length.

The above command will print the string, beginning with character number ‘1’ and up to character number ‘length($0)-2,’ to remove the last two characters in the string.

Remove Both First and Last Characters from String

To remove both the first and the last characters from the string “hello, how are you?” the command would be:

Where length($0)-2 means deducting ‘2’ from the total character length.

The above command will print the string, beginning with character number ‘2’ up to character number ‘length($0)-2,’ to remove the first and last character.

Remove Character from String Using cut

Cut is a command-line tool commonly used to extract a portion of text from a string or file and print the result to a standard output. You can also use this command for removing characters from a string.

For demonstration purposes, we will use a sample string and then pipe it to the cut command.

Remove First Character from String

To remove out the first character from the string, “hello, how are you?” the command would be:

This command will print the string, beginning with the second character, while removing the first character.

Remove First Four Characters from String

To remove the first four characters from the string “hello, how are you?” the command would be:

This command will print the string, beginning from the fifth character, while removing the first four characters.

To print the string “hello, how are you?” between the second and fifth characters, the command would be:

This command will print the string, beginning from the second character and up to the fifth character, while removing the remaining beginning and ending characters.

Remove Last Character from String

To remove the last character from the string “hello, how are you?” use the cut command with rev, as follows:

This command works by first reversing the string, then cutting the first character, and finally reversing it again to give you the desired output.

Remove Last Four Characters from String

To remove the last four characters from the line “hello, how are you?” the command would be:

This command works by first reversing the string, then cutting the first four characters, and then reversing it again to give you the desired output.

Remove First and Last Characters from String

To remove the first and last characters from the string “hello, how are you?” use the cut command with rev, as follows:

This command works by cutting the first character, then reversing the string and cutting its first character, and then reversing it again to give you the desired output.

Remove Character from String Using tr

The tr command (short for translate) is used to translate, squeeze, and delete characters from a string. You can also use tr to remove characters from a string.

For demonstration purposes, we will use a sample string and then pipe it to the tr command.

Remove All Occurrences of Character

Using the tr command, you can remove all occurrences of lowercase or uppercase characters from your string. For instance, to remove all occurrences of the lowercase character ‘h’ from the string, the command would be:

Читайте также:  Konica minolta c287seriesps картридж

Similarly, to remove all occurrences of the uppercase character ‘H’ from the string, the command would be:

You can also use interpreted sequences to remove lowercase or uppercase letters:

Remove All Occurrences of Lowercase and Uppercase Characters

You can also remove all occurrences of both lowercase and uppercase characters from a string. For instance, the following command will remove all occurrences of the character ‘h,’ both lowercase and uppercase.

Remove All Occurrences of Characters in a Specific Range

To remove all occurrences of characters from a string in the specific range ‘d-h,’ the command would be:

This command will remove all characters in the range ‘d-h’ (d,e,f,g,h) in the string.

Conclusion

In Linux, there will always be more than one way to accomplish a simple job. The same is true with removing characters from a string. This article showed you four different ways to do so, along with a few examples for removing unwanted characters from a string. Deciding which tool to use all depends on your preferences and, more importantly, on what you want to achieve.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.

Источник

Bash Remove Last x Characters From String

One might have to delete letters from either a string sometimes. Just what the case may be, Linux includes several built-in, useful tools for removing letters form such a text in Bash. This article demonstrates how to delete letters from either a string using those methods. In this post, the instructions were run on Ubuntu 20.04 Focal Fossa. The very same instructions may be run on any Linux system that has the utilities mentioned above installed. To execute the instructions, we’ll utilize the usual Terminal. The Ctrl+Alt+T shortcut will open the Terminal tool.

Method 01: Substring Way

Our first method to remove letters or characters from a string is more like creating a substring from an original one. Meanwhile, the terminal has been opened already; we will make a bash file to add our bash code. So that we can do character removal or substring making in it. So, we have used the built-in touch instruction in our shell to create a bash file.

As the file has been quickly generated in the home folder of Ubuntu 20.04, open it in some editor to edit. So, we choose GNU editor to open the file.sh document as below.

Copy the code shown below in it. This code contains bash extension at the start, and after that, we have declared a string variable “val” with a string value. At the other line, we use the “echo” phrase to display this variable in the terminal. The real task begins from here. We have initialized a variable “new” and assign it a value which is a substring of the original variable “val”. We have done it by mentioning “-14” in the braces after double colons. This tells the compiler that it has to remove the last 14 letters from the original string “FirstWorldCountries”. The remaining letters will be saved into the variable “new”. In the last line, the “echo” has been used to print the new variable “new”.

The proper execution of a file “file.sh” using the “bash” command comes out as expected. Firstly, it displays the value of the first string variable “val”, and after that, It displays the value of the newly created string from a first variable as per shown output.

Method 02: Using Special Symbols

Another simple and easier method to remove the last letters or characters from any string is via the special symbols or characters, e.g., percentage and question mark symbols. So, this time we will be using percentage and question-mark to remove the characters from any string. Hence, we have already opened the same file to update the bash script using a “GNU Nano” editor. Overall code is the same, but the variable “new” part is a little different. We have used a percentage sign to let the system know that the mentioned numbers of question marks represent the number of characters from a variable “val” to be removed after this percentage sign. You can see we have added 9 question mark symbols. This means the last 9 characters from the string “FirstWorldCountries” will be removed, and the remaining string will be “FirstWorld”. This remaining string will then be saved to a variable “new”.

Читайте также:  Как сделать файл не только для чтения linux

When we have executed the updated bash file, the output comes as anticipated. It shows the original string from the first variable and the value of the second variable, “new” which has been created from the variable “val”.

Method 03: Using Sed

Sed is a useful and effective tool for altering text sequences. That’s a non-interactive development environment that lets you work with data input and do simple text transformations. You may also use sed to delete letters from texts that you don’t want. We’ll use an example string and route it into the sed command for illustration purposes. You may delete a particular character from some kind of string with sed. So, we have used the simple line of a string within the echo statement. We have used “sed” to remove the letter “A” from the mentioned string. Make sure to follow the syntax ‘s/string_to_be_removed//’. The output shows the letter “A” has been removed.

To remove the whole word “Aqsa” we have mentioned the first and last character of a word with the dots within to represent missing letters. The output shows the string with the removal of the word “Aqsa”.

To remove any number of last characters from a string, mention the number of dots as per your requirement before the dollar symbol as shown.

Method 04: Using Awk

Awk is a sophisticated scripting language that may be used to match patterns and process texts. You may use Awk to shift and modify input in a variety of different ways. You may also delete letters from strings using awk. Awk seems a little different from “sed”. This time we have changed the string with “Aqsa Yasin”. The awk function will make substring via the substr method and print it in the terminal. The function length has been used to demonstrate the number of letters removed from the mentioned string. Here, “length($0)-5” means to remove the last 5 characters of a string, and the remaining will be a part of a substring to be printed out.

We have tried to remove the last 9 characters from a string “Aqsa Yasin” and got “A” as the output substring.

Method 05: Using Cut

Cut seems to be a command-line utility for extracting a piece of text from such a phrase or document and printing it to standard output. This operation can also be used to remove letters from some kind of string. We’ll use an example phrase and pass it to the cut instruction for testing purposes. So we have used the “Aqsa Yasin” phrase and passed it to the “cut” query. After the flag –c, we have defined the range of indexes for a string to cut the characters from a string mentioned. It will show the characters from index 1 to index 5. Index 5 has been excluded here. The output shows the first 4 characters as “Aqsa”.

This time we will use the cut instruction differently. We have used the “rev” function to reverse the string. After the reverse of a string, we will be cutting the first character from a string. The flag “-c2-“means our substring will be onward character 2 . After that, the reverse function is again used to revert the string. So, this time we got the original string back with the removal of the last character.

To remove the last 7 characters, you just have to mention “-c7-“in the cut command while using the reverse function as well.

Conclusion:

There is time more than one method to do a basic task on Linux. Similarly, deleting characters out of a text is possible. This article demonstrated five distinct methods for eliminating undesired characters out of a string, as well as some instances. Whatever tool you choose is entirely dependent on your choice and, more crucially, what you’d like to accomplish.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Источник

Поделиться с друзьями
КомпСовет
Adblock
detector