increment output of pipe by 1 in shell

Viewed 276

eg: echo "abcdef_312345" | cut -b 9 --> output of this will be 1, so I want to increase that by 1 in the same line

echo "abcdef_312345" | cut -b 9 | <command to increment by 1>
3 Answers

Eliminate the subprocess overhead:

$ x="abcdef_312456"
$ y=$(( ${x:8:1} + 1 ))
$ echo "${y}"
2

You can do :

echo "abcdef_312345" | cut -b 9 | awk '{print $1 + 1}'

But cut | awk is an anti-pattern, so you should do:

echo "abcdef_312345" | awk '{print $9 + 1}' FS=

Try

echo "abcdef_312345" | cut -b 9 | expr "$(cat)" + 1
Related