If you have a file in which every line is
an eleven-character (or whatever) string that you want to chop up,
sed
is the tool to use.
It’s fine for manipulating a single string, but it’s overkill.
For a single string, Jason’s answer is probably the best,
if you have access to bash version 4.2 or higher.
However, the ${parameter:offset}
and ${parameter:offset:length}
syntaxes
appear to be unique to bash (well, bash, ksh93, mksh, and zsh) —
I don’t see them
in The Open Group Base Specifications for Shell Command Language.
If you’re stuck with a POSIX-compliant shell
that doesn’t support substring expansion (extraction), you can use
$ printf "%s\n" "${string#???}"
W2345678
$ printf "%s\n" "${string%????}"
H08W234
using printf
instead of echo
to guard against strings like abc-e
,
where, when you drop the first three characters, you are left with -e
(and echo -e
doesn’t do what you would want).
And, if you’re not using a Bourne-family shell at all
(or you’re using an ancient, pre-POSIX system), these should still work:
$ expr " $string" : ' ...\(.*\)'
W2345678
$ expr " $string" : ' \(.*\)....'
H08W234
The extra leading space is to avoid problems with values of $string
that are actual expr
operators (e.g., +
, /
, index
or match
)
or options (e.g., --
, --help
or --version
).
sed
?H08W2345678
and need to manipulate it toW2345678
This value with other datum will be put into an email sent off. Thie emailing will be undertaken with cron.awk
ing it. I create an array and then modify each of the element within the array (all differently - i.e change the Epoch timestaimp in seconds to a date etc.)printf %s\\n "XX,H08W2345678,YY" | awk -F, '{print substr($2, 4); print substr($2, 1, length($2)-4)}'