You are not logged in.

#1 2010-06-11 13:56:32

devil_kc
Member
Registered: 2008-09-14
Posts: 93

help with a script

hi guys, i need help smile

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

#2 2010-06-11 14:22:47

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: help with a script

Does sed 's/\([01]{10}\)0\([01]{21}\)/\11\2/g' work?

Offline

#3 2010-06-11 14:26:19

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: help with a script

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

#4 2010-06-11 14:30:22

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: help with a script

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)

Offline

#5 2010-06-11 14:36:44

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: help with a script

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 sad

Last edited by Bralkein (2010-06-11 14:37:49)

Offline

Board footer

Powered by FluxBB