tags:

views:

110

answers:

4

I have a file with line: name, space and string of zero and one, and I need to extract every 5nth character of the string of zero and one, make a sum of the results and if the sum is not 0 - save the name into the other file.

1rt2 0001000000100000000000001010000100000000010000001000000100010010000000000000
1gh4 0001000000100000000000001010000100000000010000001000000100000010000000000000
3fg5 1000000100010010000000000000100000010000000001000000100000010000000000000000
45gh 1000000100000010000000000000100000010000000001000000000100010000000000000000

The question: how to extract every 5th number (letter) of the bitstring? The more simplistic the solution - the better...

Thank's a lot in advance!

A: 

Try ....(.).

  • .... matches four arbitrary letters
  • (.) matches the fifth letter and captures it.

I'm not sure about sed/awk regexp syntax, so you might have to escape the parens.

phimuemue
A: 

Reads from stdin:

#!/bin/bash

while read name bits; do
    (($(sed 's/....(.)/$1/g' <<< "$bits") > 0)) && echo "$name"
done

Reads each line, uses sed to extract every fifth bit, then compares that number with ((num > 0)). Read the && as if-then: if the number is greater than zero then echo the name.

John Kugelman
+1  A: 
sed -e '/ \(.........\)*........1/s/^\([^ ]*\) .*$/\1/;t;d'

Match the lines that have a 1 on a position that is a multiple of 9 (for these lines the sum will not be 0) and print the filename portion of that line. All other lines are not printed.

sed -e '/ \(.....\)*....1/s/^\([^ ]*\) .*$/\1/;t;d'

will do the job for every 5th 0 or 1.

Bart Sas
+1 To generalize your selector pattern: `/ \([01]\{5\}\)*[01]\{4\}1/`
Dennis Williamson
A: 
awk '{
 name=$1
 m=split($2,n,"")
 for(o=1;o<=m;o+=4){
   total+=n[o]
 }
 if(total>0){
    print name > "file"
 }
}' file
ghostdog74