| by Arround The Web | No comments

Bash Remove Last x Characters From String?

There are several ways or commands in Linux that we may use to remove the last characters from a string value in bash, including “cut”, “sed” “compliment”, “awk” and others. The “%” and “?” operators can also be employed to eliminate the ending element from a string. By utilizing the “cut” command in bash, we can also delete a specific character from a string that we want to eliminate. In this article, we will show a variety of techniques for deleting elements from sequences.

Example # 1: Removing the Last Characters from a String in Bash

In this example, we will use the “%” and “?” symbols in the command to eliminate the final characters from a string in bash. Before employing these symbols in commands, we will first make sure you know that they will eliminate the last few characters from the string and adjust the number of characters according to the scenario. Depending on what we want to eliminate, we can cut the string’s last segments off in any number of ways.

Let us now begin to implement the command to remove the last characters from a string and display the standard output in the terminal window. The keyword variable will be used in the first and the list string will be assigned to it. In bash, a variable can store a number, a particular word, or a set of characters. You can construct a variable by giving a value to its reference without having to declare it. If you want to read the data from a variable, you must use the symbol “$” before the variable name.

The value of the string we have is “I Love Programming languages”. Therefore, after typing the variable, we must add the string inside of the inverted commas. In the command, we did not give any space. In this line, the string contains four words. And in this section, we are going to remove the last word, “Languages”. Then, in the following line, we use the “echo” statement followed by the dollar sign “$”. Inside of echo, we use curly brackets and inside of curly brackets, we pass the string that is stored in the “variable” as an argument. Then, we use the symbol “%” to remove the last characters from the string followed by the symbol “?” to indicate how many last characters we want to remove. Consequently, we will eliminate the nine-letter word “languages” from this line. So, after the percentage symbol, we enter nine question marks.

Linux@linux:~$ variable=”Iloveprogramminglanguages”

Linux@linux:~$ echo ${variable%?????????}

As you can see in the image below, after running this command, the result is presented without the word “languages”. The displayed string value is now three words “ILoveProgramming” there is no space after the words as there was none in the given value.

Now, in the next section, we will remove the first few characters or the first word from the string by using the symbol “#,” which is used to remove the characters from strings at the beginning. So, for this, we are going to use the same command. First, we pass the string “I Am Going To USA” inside of inverted commas, which is stored in the created “variable”. Then, in the following step, we use the “echo” command, inside of it we use the “$” symbol.

Next, we use curly brackets, inside of these brackets since the string is saved there. So, we pass the “variable” and then we use the hash “#” symbol. After that, we type four “?” question marks, which will delete the first four characters from the string. Here, space is also considered a character.

Linux@linux:~$ variable=”I Am Going To USA”

Linux@linux:~$ echo ${variable#????}

Now that the first two words of the string, “I Am,” have been removed, you can see in the output that it has displayed the remaining three words from the phrase “Going to USA.” We used four question marks in the above output because there is a space between these two words and spaces are treated as characters in this context so in total the removed characters are four.

Example # 2: Using Bash’s Parameter Replacement to Remove the Last Characters from a String.

In this section, we will remove the last word from the string by specifying its length in bash. We can employ the “#” operation to determine the length of a string. To do this, we must first wrap the variable name in inverted commas before using the “#” operation to determine the string’s length. Consequently, we can determine the length of the string variable in Unix by using the “#” operator.

Now, in the command, we first use the created “variable” and we pass the value of the string inside the inverted commas which is “Keep it up”. There are a total of three words in the value. Then, in the next line, we pass the parameter “length” and we store the argument “variable” in which the value of the string is stored in it but before passing “variable” we must use the “$” before using curly brackets and inside of this bracket we must use the “#” operator.

Then, on the following line, we type the command “echo,” followed by the dollar sign ($), the curly brackets, the parameter “variable,” the double colon (“::”), the argument length, and the number of characters we need to erase, which is two and in the form of the symbol “-2”.

Linux@linux:~$ variable=”Keep it up”

Linux@linux:~$ length= ${#variable#}

Linux@linux:~$ echo ${variable: : length -2}

Because we provided the length of character two, so it erased the last two characters from the string and the displayed output contained the string value “Keep it”.

Example # 3: Utilizing the Cut Command

In bash, the cut command can be used to remove certain words or characters from strings or files. Let us use the cut command to do this. First, type “echo,” then use inverted commas to pass the string value “I Love My Mother,” then use this bar symbol “|”. Next, type “cut,” then use “-c” to cut the character and then provide the character range we want to display on the output. In this case, we used the range “1 to 10,” which will display the first ten characters from the string value here, the spaces are a part of characters.

Linux@linux:~$ echo “I Love My Mother” | cut –c 1-10

As you can see in the image below when we run this command, it only displayed the initial characters, including the spaces, and the remaining characters, which are the last ones were removed. The string values containing “I Love My” were displayed as a result of the “1 to 10” range that we provided.

Example # 4: Using the sed Command

In this section, we will use the “sed” command to eliminate the string’s last few characters. The sed function has three major applications in the bash script: writing to stdout, erasing content, and changing a particular string. We will use the same command as in the previous example but this time the string’s value will be changed to “I love coding” without any spaces.

Next, the part after the bar symbol “|” will be changed because this part uses the “sed” command, so type “sed” into the terminal. Finally, we will use the single inverted comma and inside of this we use “s/.$/”. This will eliminate the string’s end word. You can also remove a specific character from the string by substituting it for “$” in the command that you want to remove the character from.

Linux@linux:~$ echo “Ilovecoding” | sed ‘s/.$//

The character “g” the last character of the value has been removed from the output, which now shows the string value “Ilovecodin.”

Conclusion

In this article, we have examined how to use bash to eliminate a single character from a line and how to eliminate the last letter from a given string. Using the “%” and “?” operators, we eliminated the last word from the string in the first example. In the second example, we provided the length of the characters we wanted to eliminate from the string value in the command. Then, in the third example, we gave the number of characters we wanted to display using the “cut” command and deleted the last words. Finally, in the final section, we covered the “sed” function.

Share Button

Source: linuxhint.com

Leave a Reply