Pergunta

I want to add one to the last value at the end of a string in sed. I'm thinking along the lines of

cat 0809_data.csv |sed -e 's/\([0-9]\{6\}\).*\(,[^,]*$\)/\1\2/g'| export YEARS = $(echo `grep -o '[^,]*$' + 1`|bc)

e.g. 123456, kjhsflk, lksjgrlks, 2.8 -> 123456, 3.8

Would this be more reasonable/feasible in awk?

Foi útil?

Solução

This should work:

years=$(awk -F, 'BEGIN{ OFS=", "} {print $1, $4+1}' 0809_data.csv)

It would be really awkward to try to use sed and do arithmetic with part of the result. You'd have to pull the string apart and do the math and put everything back together. AWK does that neatly without any fuss.

Notice that cat is not necessary (even using sed in a command similar to the one in your question) and it's probably not necessary to export the variable unless you're calling another script and need it to be able to access it as a "global" variable. Also, shells generally do integer math so you don't need to use bc unless you need floats.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top