Remove first character of a string in Bash

Bash

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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.

Solution 2 - Bash

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

Solution 3 - Bash

remove first n characters from a line or string

#method1) using bash

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

#method2) using cut

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

#method3) using sed

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

#method4) using awk

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

Solution 4 - Bash

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

Solution 5 - Bash

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

Solution 6 - Bash

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  -

Solution 7 - Bash

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

Solution 8 - Bash

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosiPView Question on Stackoverflow
Solution 1 - BashLiXCEView Answer on Stackoverflow
Solution 2 - BashfulmicotonView Answer on Stackoverflow
Solution 3 - BashhankyoView Answer on Stackoverflow
Solution 4 - Bashl0b0View Answer on Stackoverflow
Solution 5 - BashRafael OliveiraView Answer on Stackoverflow
Solution 6 - BashFredrik PihlView Answer on Stackoverflow
Solution 7 - BashmikeservView Answer on Stackoverflow
Solution 8 - BashIvanView Answer on Stackoverflow