How to 'drop'/delete characters from in front of a string?
Just using bash (or ksh93
where that syntax comes from or zsh
):
string="H08W2345678"
echo "${string:3}"
W2345678
echo "${string:0:-4}"
H08W234
See the Wooledge wiki for more on string manipulation.
$ echo "H08W2345678" | sed 's/^.\{3\}//'
W2345678
sed 's/^.\{3\}//'
will find the first three characters by ^.\{3\}
and replace with blank. Here ^.
will match any character at the start of the string (^
indicates the start of the string) and \{3\}
will match the the previous pattern exactly 3 times. So, ^.\{3\}
will match the first three characters.
$ echo "H08W2345678" | sed 's/.\{4\}$//'
H08W234
Similarly, sed 's/.\{4\}$//'
will replace the last four characters with blank ($
indicates the end of the string).
How to 'drop'/delete characters from in front of a string?
I have a string that I would like to manipulate. The string is H08W2345678 how would I be able to manipulate it so the output is just W2345678?
echo "H08W2345678" | cut -c 4-
Similarly if the I wanted to drop the last 4 characters from H08W2345678 so that I get H08W234 how would I do this?
echo "H08W2345678" | cut -c -7