如何从 bash grep 输出中删除最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5074893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:13:00  来源:igfitidea点击:

How to remove the last character from a bash grep output

bashgrep

提问by liv2hak

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.

我想要做的是删除尾随的“;” 以及。我怎样才能做到这一点?我是 bash 的初学者。任何想法或建议都会有所帮助。

采纳答案by Laurence Gonsalves

I'd use sed 's/;$//'. eg:

我会用sed 's/;$//'. 例如:

COMPANY_NAME=`cat file.txt | grep "company_name" | cut -d '=' -f 2 | sed 's/;$//'`

回答by ztank1013

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

这将删除 COMPANY_NAME 变量中包含的最后一个字符,无论它是否是分号:

echo "$COMPANY_NAME" | rev | cut -c 2- | rev

回答by grokster

foo="hello world"
echo ${foo%?}
hello worl

回答by Aaron J Lang

I'd use head --bytes -1, or head -c-1for short.

我会使用head --bytes -1,或head -c-1简称。

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

headoutputs only the beginning of a stream or file. Typically it counts lines, but it can be made to count characters/bytes instead. head --bytes 10will output the first ten characters, but head --bytes -10will output everything except the last ten.

head仅输出流或文件的开头。通常它计算行数,但可以改为计算字符/字节数。head --bytes 10将输出前十个字符,但head --bytes -10将输出除最后十个字符以外的所有字符。

NB: you may have issues if the final character is multi-byte, but a semi-colon isn't

注意:如果最后一个字符是多字节的,你可能会遇到问题,但分号不是

I'd recommend this solution over sedor cutbecause

我会推荐这个解决方案sed或者cut因为

  • It's exactly what headwas designed to do, thus less command-line options and an easier-to-read command
  • It saves you having to think about regular expressions, which are cool/powerful but often overkill
  • It saves your machine having to think about regular expressions, so will be imperceptibly faster
  • 这正是head设计的目的,因此命令行选项更少,命令更易于阅读
  • 它使您不必考虑正则表达式,这些表达式很酷/功能强大但通常矫枉过正
  • 它使您的机器不必考虑正则表达式,因此会不知不觉地更快

回答by mightypile

I believe the cleanest way to strip a single character from a string with bash is:

我相信用 bash 从字符串中去除单个字符的最干净的方法是:

echo ${COMPANY_NAME:: -1}

but I haven't been able to embed the grep piece within the curly braces, so your particular task becomes a two-liner:

但我无法将 grep 嵌入花括号中,因此您的特定任务变成了两行:

COMPANY_NAME=$(grep "company_name" file.txt); COMPANY_NAME=${COMPANY_NAME:: -1} 

This will strip any character, semicolon or not, but can get rid of the semicolon specifically, too. To remove ALL semicolons, wherever they may fall:

这将删除任何字符,分号与否,但也可以专门删除分号。要删除所有分号,无论它们在哪里:

echo ${COMPANY_NAME/;/}

To remove only a semicolon at the end:

只删除末尾的分号:

echo ${COMPANY_NAME%;}

Or, to remove multiple semicolons from the end:

或者,从末尾删除多个分号:

echo ${COMPANY_NAME%%;}

For great detail and more on this approach, The Linux Documentation Project covers a lot of ground at http://tldp.org/LDP/abs/html/string-manipulation.html

有关此方法的详细信息和更多信息,Linux 文档项目在http://tldp.org/LDP/abs/html/string-manipulation.html 上涵盖了很多内容

回答by Det

Using sed, if you don't know what the last character actually is:

使用sed, 如果您不知道最后一个字符实际上是什么:

$ grep company_name file.txt | cut -d '=' -f2 | sed 's/.$//'
"Abc Inc"

回答by Has QUIT--Anony-Mousse

Don't abuse cats. Did you know that grepcan read files, too?

不要滥用cats。你知道它grep也可以读取文件吗?

The canonical approach would be this:

规范的方法是这样的:

grep "company_name" file.txt | cut -d '=' -f 2 | sed -e 's/;$//'

the smarter approach would use a single perlor awkstatement, which can do filter and different transformations at once. For example something like this:

更聪明的方法是使用单个perlorawk语句,它可以一次进行过滤和不同的转换。例如这样的事情:

COMPANY_NAME=$( perl -ne '/company_name=(.*);/ && print ' file.txt )

回答by kurumi

don't have to chain so many tools. Just one awk command does the job

不必链接这么多工具。只需一个 awk 命令即可完成这项工作

 COMPANY_NAME=$(awk -F"=" '/company_name/{gsub(/;$/,"",) ;print }' file.txt)

回答by Jens Bodal

Assuming the quotation marks are actually part of the output, couldn't you just use the -o switch to return everything between the quote marks?

假设引号实际上是输出的一部分,难道您不能只使用 -o 开关来返回引号之间的所有内容吗?

COMPANY_NAME="\"ABC Inc\";" | echo $COMPANY_NAME | grep -o "\"*.*\""

回答by Paused until further notice.

In Bash using only one external utility:

在仅使用一个外部实用程序的 Bash 中:

IFS='= ' read -r discard COMPANY_NAME <<< $(grep "company_name" file.txt)
COMPANY_NAME=${COMPANY_NAME/%?}