You are not logged in.
Pages: 1
hi guys, i need help
i'd need a bash script to change every eleventh number in a string from zero to one
exapmple
i have 00100000000000000000000000000000
and i need a script to change it to 001000000010000000000000000000
(32 digits)
those ^^ are bits in a pgsql base. every entry has this string. and there are few thousands entries
could anybody help? me and a friend were thinking about maybe awk to extract first 10 digits into a file, change eleventh digit and then use awk to copy next 21 digits.
is that possible with pgsql maybe?
thanx
Offline
Does sed 's/\([01]{10}\)0\([01]{21}\)/\11\2/g' work?
Offline
imo, you would probably have better luck in another language other than Bash.
I would suggest a python script, or maybe a perl script.
also, just for clarification. you want every 11'th digit that is a zero to become a one, does that mean that if every 11'th digit is one you would like to toggle it back to zero? or just have it remain a 1? I think if you just apply a bit-mask with the proper logic expression you should be able to get the desired results.
probably using an OR logic evaluation.
0010 0000 0000 0000 0000 0000 0000 0000 //example string.
OR 0000 0000 0010 0000 0000 0100 0000 0000 //every 11th is a one. this is the bit-mask.
-----------------------------------------------------------------------
0010 0000 0010 0000 0000 0100 0000 0000
I'll leave the coding and research to you. but I think this would be a better approach than the way you were suggesting.
Last edited by Cyrusm (2010-06-11 14:27:22)
Hofstadter's Law:
It always takes longer than you expect, even when you take into account Hofstadter's Law.
Offline
pure bash and generalized for easy reuse:
#!/bin/bash
# $1 = position of character to change
# $2 = replacement character
# $3 = text to change
changeNthToX() {
local n="$1" x="$2" text="$3" start end
start="${text:0:n-1}"
end="${text[@]:n}"
echo "${start}${x}${end}"
}
### changing one string
string='00100000000000000000000000000000'
changeNthToX 11 1 "$string"
### or lines from a file
while IFS=$'\n' read -r; do
changeNthToX 11 1 "$REPLY"
done < ./file
Last edited by brisbin33 (2010-06-11 19:34:28)
//github/
Offline
I think this should work for your string:
sed 's/\(.\{10\}\).\(.\{21\}\)/\11\2/g'
Remove the g at the end if you only want it to replace the first occurrence on each line. I'm not sure if that gives you a complete solution to what you're trying to do, but hopefully it will be useful.
Edit: Bah sorry when I started writing this I saw no replies
Last edited by Bralkein (2010-06-11 14:37:49)
Offline
Pages: 1