Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 2.13 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Split large string into substrings
  2. kent$  echo "abcdefghijklmnopqr"|sed -r 's/(.{5})/1 /g'
  3. abcde fghij klmno pqr
  4.        
  5. kent$  echo "abcdefghijklmnopqr"|sed -r 's/(.{5})/1n/g'
  6. abcde
  7. fghij
  8. klmno
  9. pqr
  10.        
  11. kent$  echo "abcdefghijklmnopqr"|awk '{while(length($0)>=5){print substr($0,1,5);gsub(/^./,"")}}'
  12.  
  13. abcde
  14. bcdef
  15. cdefg
  16. defgh
  17. efghi
  18. fghij
  19. ghijk
  20. hijkl
  21. ijklm
  22. jklmn
  23. klmno
  24. lmnop
  25. mnopq
  26. nopqr
  27.        
  28. s=ABCDEFGHIJ
  29. for (( i=0; i < ${#s}-4; i++ )); do
  30.   printf ">%dn%sn" $((i+1)) ${s:$i:5}
  31. done
  32.        
  33. >1
  34. ABCDE
  35. >2
  36. BCDEF
  37. >3
  38. CDEFG
  39. >4
  40. DEFGH
  41. >5
  42. EFGHI
  43. >6
  44. FGHIJ
  45.        
  46. ${string:position:length}
  47.        
  48. stringZ=abcABC123ABCabc
  49. #       0123456789.....
  50. #       0-based indexing.
  51.  
  52. echo ${stringZ:0}                            # abcABC123ABCabc
  53. echo ${stringZ:1}                            # bcABC123ABCabc
  54. echo ${stringZ:7}                            # 23ABCabc
  55.  
  56. echo ${stringZ:0:5}                          # abcAB
  57.                                              # Five characters of substring.
  58.        
  59. for i in seq 0 ${#stringZ}; do
  60.     echo ${stringZ:$i:5}
  61. done
  62.        
  63. $ sed 's/(.....)/1n/g' < filecontaininghugestring
  64.        
  65. str=ABCDEFGHIJKLM
  66. splitfive(){ echo "${1:$2:5}" ; }
  67. for (( i=0 ; i < ${#str} ; i++ )) ; do splitfive "$str" $i ; done
  68.        
  69. #!/usr/bin/env bash
  70.  
  71. splitstr(){
  72.     printf '%sn' "${1:$2:$3}"
  73. }
  74.  
  75. n=$1
  76. offset=$2
  77.  
  78. declare -a by_fives
  79.  
  80. while IFS= read -r str ; do
  81.     for (( i=0 ; i < ${#str} ; i++ )) ; do
  82.             by_fives=("${by_fives[@]}" "$(splitstr "$str" $i $n)")
  83.     done
  84. done
  85.  
  86. echo ${by_fives[$offset]}
  87.        
  88. $ split-by 5 2 <<<"ABCDEFGHIJKLM"
  89. CDEFG
  90.        
  91. #include <stdio.h>
  92.  
  93. int main(void){
  94.     FILE* f;
  95.     int n=0;
  96.     char five[6];
  97.  
  98.     five[5] = '';
  99.  
  100.     f = fopen("inputfile", "r");
  101.  
  102.     if(f!=0){
  103.             fread(&five, sizeof(char), 5, f);
  104.             while(!feof(f)){
  105.                     printf("%sn", five);
  106.                     fseek(f, ++n, SEEK_SET);
  107.  
  108.                     fread(&five, sizeof(char), 5, f);
  109.             }
  110.     }
  111.  
  112.     return 0;
  113. }
  114.        
  115. sed -nr ':a;h;s/(.{5}).*/1/p;g;s/.//;ta;' <<<"ABCDEFGHIJKLM" | # split string
  116.      sed '=' | sed '1~2s/^/>/' # add line numbers and insert '>'
  117.        
  118. $ ls
  119.  
  120. $ echo "abcdefghijklmnopqr" | split -b5
  121.  
  122. $ ls
  123. xaa  xab  xac  xad
  124.  
  125. $ cat xaa
  126. abcde