Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the last character from a bash grep output

Tags:

grep

bash

COMPANY_NAME=`cat file.txt | grep "company_name" | cut -d '=' -f 2`  

outputs something like this

"Abc Inc"; 

What I want to do is I want to remove the trailing ";" as well. How can i do that? I am a beginner to bash. Any thoughts or suggestions would be helpful.

like image 582
liv2hak Avatar asked Feb 22 '11 06:02

liv2hak


People also ask

How do you remove the last character in Bash?

Two Bash parameter expansion techniques can help us to remove the last character from a variable: Substring expansion – ${VAR:offset:length} Removing matching suffix pattern – ${VAR%word}

How do I delete a specific character in Bash?

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.

How do I remove the last character of a line in Unix?

The sed is a free and open-source stream editor for Linux, macOS, *BSD, and Unix-like systems. It is perfect for removing/deleting the last character and perform other options on your files or shell variables.

How do I remove the last 3 characters from a string in Linux?

In this method, you have to use the rev command. The rev command is used to reverse the line of string characterwise. Here, the rev command will reverse the string, and then the -c option will remove the first character. After this, the rev command will reverse the string again and you will get your output.


1 Answers

This will remove the last character contained in your COMPANY_NAME var regardless if it is or not a semicolon:

echo "$COMPANY_NAME" | rev | cut -c 2- | rev 
like image 172
ztank1013 Avatar answered Oct 24 '22 09:10

ztank1013