Home > Blockchain >  increment output of pipe by 1 in shell
increment output of pipe by 1 in shell

Time:03-10

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>

CodePudding user response:

Eliminate the subprocess overhead:

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

CodePudding user response:

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=

CodePudding user response:

Try

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