在 Bash 中删除字符串的第一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6594085/
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:43:55  来源:igfitidea点击:

Remove first character of a string in Bash

bash

提问by JosiP

I need to calculate md5sum of one string (pathfile) per line in my lsdump, directory_listing_file:

我需要计算在我的每行一个字符串(pathfile)的的md5sumls转储directory_listing_file

./r/g4/f1.JPG
./r/g4/f2.JPG
./r/g4/f3.JPG
./r/g4/f4.JPG

But that md5sum should be calculated without the initial dot. I've written a simple script:

但是 md5sum 应该在没有初始点的情况下计算。我写了一个简单的脚本:

while read line
do
    echo $line | exec 'md5sum'
done

./g.sh < directory_listnitg.txt

How do I remove the first dot from each line?

如何从每行中删除第一个点?

采纳答案by l0b0

Set the field separator to the path separator and read everything except the stuff before the first slash into $name:

将字段分隔符设置为路径分隔符,并读取除第一个斜杠之前的内容之外的所有内容$name

while IFS=/ read junk name
do
    echo $name
done < directory_listing.txt

回答by LiXCE

myString="${myString:1}"

Starting at character number 1 of myString (character 0 being the left-most character) return the remainder of the string. The "s allow for spaces in the string. For more information on that aspect look at $IFS.

从 myString 的第 1 个字符(字符 0 是最左边的字符)开始返回字符串的其余部分。"s 允许字符串中有空格。有关这方面的更多信息,请查看 $IFS。

回答by fulmicoton

You can pipe it to

你可以用管道把它

cut -c2-

Which gives you

这给你

while read line
do
echo $line | cut -c2- | md5sum
done

./g.sh < directory_listnitg.txt

回答by mikeserv

You can do the entire thing like this:

你可以像这样完成整个事情:

% sh -c `sed 's@^.\(.*\)@md5sum @' <./dirlist.txt`

Really, I'm thinking you can make this a lot more efficient, but I don't know what is generating your list. If you can pipeit from that, or run that command through a heredocto keep its output sane, you can do this whole job streamed, probably.

真的,我认为您可以提高效率,但我不知道是什么生成了您的列表。如果您可以pipe这样做,或者通过 a 运行该命令heredoc以保持其输出正常,那么您可能可以通过流式传输完成整个工作。

EDIT:

编辑:

OK, you say it's from an "ls dump." Well, here's something a little flexible:

好吧,你说它来自“ls dump”。好吧,这里有一些灵活的东西:

% ls_dump() {
> sed 's@^.\(.*\)$@md5sum @' <<_EOF_ | sh -s
>> `ls ${@}`
>> _EOF_
> }
% ls_dump -all -args -you /would/normally/give/ls
<desired output>

I think this calls only a single subshell in total. It should be pretty good, but in my opinion, find ... -exec md5sum {} ... +is probably safer, faster, and all you need.

我认为这总共只调用了一个子shell。它应该相当不错,但在我看来,find ... -exec md5sum {} ... +它可能更安全、更快,并且满足您的所有需求。

EDIT2:

编辑2:

OK, so now I will actually answer the question. To remove the first character of a string in any POSIX compatible shell you need only look to parameter expansion like:

好的,现在我来实际回答这个问题。要在任何 POSIX 兼容 shell 中删除字符串的第一个字符,您只需要查看如下参数扩展:

${string#?}

-Mike

-麦克风

回答by Fredrik Pihl

Different approach, using sed, which has the benefit that it can handle input that doesn't start with a dot. Also, you won't run into problems with echoappending a newline to the output, which will cause md5sum to report bogus result.

不同的方法,使用 sed,它的好处是它可以处理不以点开头的输入。此外,您不会遇到echo在输出中附加换行符的问题,这将导致 md5sum 报告虚假结果。

#!/bin/bash

while read line
do
     echo -n $line | sed 's/^.//' | md5sum
done < input

compare these:

比较这些:

$ echo "a" | md5sum
60b725f10c9c85c70d97880dfe8191b3  -

$ echo -n "a" | md5sum
0cc175b9c0f1b6a831c399e269772661  -

回答by Rafael Oliveira

There ia a very easy way to achieve this:

有一种非常简单的方法可以实现这一点:

Suppose that we don't want the prefix "i-" from the variable

假设我们不想要变量中的前缀“i-”

$ ROLE_TAG=role                                                                            
$ INSTANCE_ID=i-123456789

You just need to add '#'+[your_exclusion_pattern], e.g:

您只需要添加 '#'+[your_exclusion_pattern],例如:

$ MYHOSTNAME="${ROLE_TAG}-${INSTANCE_ID#i-}"  
$ echo $MYHOSTNAME
role-123456789