开发者

Remove first character of a string in Bash

I need to calculate md5sum of one string (pathfile) per line in my ls dump, 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:

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

./g.sh < directory_listnitg.txt

How do I remove the first dot from each line?


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.


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


remove first n characters from a line or string

method 1) using bash

 str="price: 9832.3"
 echo "${str:7}"

method 2) using cut

 str="price: 9832.3"
 cut -c8- <<< $str

method 3) using sed

 str="price: 9832.3"
 sed 's/^.\{7\}//' <<< $str

method 4) using awk

 str="price: 9832.3"
 awk '{gsub(/^.{7}/,"");}1' <<< $str


There ia a very easy way to achieve this:

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

$ ROLE_TAG=role                                                                            
$ INSTANCE_ID=i-123456789

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

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


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

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


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 echo appending a newline to the output, which will cause md5sum to report bogus result.

#!/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  -


Or like this

myString="${myString/.}"

Testing on Ubuntu 18.04.4 LTS, bash 4.4.20:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:    18.04
Codename:   bionic

$ echo $BASH_VERSION
4.4.20(1)-release

$ myString="./r/g4/f1.JPG"
$ myString="${myString/.}"
$ echo $myString
/r/g4/f1.JPG


You can do the entire thing like this:

% sh -c `sed 's@^.\(.*\)@md5sum \1@' <./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 pipe it from that, or run that command through a heredoc to keep its output sane, you can do this whole job streamed, probably.

EDIT:

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

% ls_dump() {
> sed 's@^.\(.*\)$@md5sum \1@' <<_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.

EDIT2:

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:

${string#?}

-Mike

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜