deadlykingdx

tool.sh

Jan 7th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.67 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #an den exei parametrous, ektupwse to am
  4. if [ $# -eq 0 ]; then
  5.     echo '1058123-'
  6.     exit
  7. fi
  8.  
  9. ############ arxi edit ##########
  10.  
  11. # an dwthoun 6 arguments
  12. if [ $# -eq 6 ]
  13. then
  14.  
  15. # checkarei an einai tupou: -f <file> --edit <id> <column> <value>
  16.     if [ "$1" = "-f" -a "$3" = "--edit" ]
  17.     then
  18.         file=$2
  19.         id=$4
  20.         column=$5
  21.         value=$6
  22.  
  23. # an i stili den einai metaksu 2-8 kanei exit
  24.         if (($column > 8 | $column < 2))
  25.         then
  26.             exit
  27.         fi
  28. # an to id uparxei, allazei ti stili pou dwthike sti timi pou dwthike
  29.         awk -v column="$column" -v id="$id" -v value="$value" 'BEGIN{FS="|";OFS="|"} { $column = ($1== id ? value : $column) }1' $file > tmp && mv tmp $file
  30.         exit
  31.  
  32. # checkarei an einai tupou: --edit <id> <column> <value> -f <file>
  33.     elif [ "$5" = "-f" -a "$1" = "--edit" ]
  34.     then
  35.         id=$2
  36.         column=$3
  37.         value=$4
  38.         file=$6
  39.         if (($column > 8 | $column < 2))
  40.         then
  41.             exit
  42.         fi
  43.  
  44. # an to id uparxei, allazei ti stili pou dwthike sti timi pou dwthike
  45.         awk -v column="$column" -v id="$id" -v value="$value" 'BEGIN{FS="|";OFS="|"} { $column = ($1== id ? value : $column) }1' $file > tmp && mv tmp $file
  46.         exit
  47.     fi
  48. fi
  49. ################# telos edit ####################
  50.  
  51. ################ arxi id    #################
  52. #an dwthei prwta -f kai meta -id
  53. if [ "$1" = "-f" -a "$3" = "-id" ]; then
  54.     file="$2"
  55.     id="$4"
  56.  
  57. #tupwnei ta pediaonoma,eponumo,imerominia genisis, tou id pou dothtike, diaxorismena me ena keno metaksu tous
  58.     awk 'BEGIN{FS="|"; OFS;} $1=='$id' {print $3,$2,$5}' $file
  59.     exit
  60.  
  61. #an dwthei prwta -id kai meta -f
  62. elif [ "$1" = "-id" -a "$3" = "-f" ]
  63. then
  64.     file="$4"
  65.     id="$2"
  66.  
  67. #tupwnei ta pediaonoma,eponumo,imerominia genisis, tou id pou dothtike, diaxorismena me ena keno metaksu tous
  68.     awk 'BEGIN{FS="|"; OFS;} $1=='$id' {print $3,$2,$5}' $file
  69.     exit
  70. elif [ "$1" = "-f" -a "$3" = "" ]
  71. then
  72.     file="$2"
  73.     cat $file | egrep -v "^#.*"
  74.     exit
  75. fi
  76. ############### telos id ##############
  77.  
  78.  
  79. options=$(getopt -o f: --long firstnames,lastnames,socialmedia,born-since:,born-until: -- "$@")
  80. eval set -- "$options"
  81.  
  82. # thetw metavlites bornsince kai bornuntil 0 gia na diksw oti den exoun dwthei kai na ginei argotera epilogi
  83. bornsince=0
  84. bornuntil=0
  85. while true; do
  86.     case "$1" in
  87.     -f ) #an arxika dwthei -f
  88.  
  89. # apothikevei to onoma tou arxeiou sti metavliti file
  90.         file=$2
  91.         shift 2
  92.         ;;
  93.  
  94. #an dwthei --firstnames
  95.     --firstnames )
  96.  
  97. # apothikeuei ti zitithike
  98.         operation=fnames # apothikeuei ti zitithike
  99.         shift;
  100.  
  101.         ;;
  102. #an dwthei --lastnames
  103.     --lastnames )
  104.  
  105. # apothikeuei ti zitithike
  106.         operation=lnames
  107.         shift;
  108.         ;;
  109.     --born-since )
  110.         operation=born
  111.         bornsince=$2
  112.         shift 2
  113.         ;;
  114.     --born-until )
  115.         operation=born
  116.         bornuntil=$2
  117.         shift 2
  118.         ;;
  119.     --socialmedia )
  120.         operation=social
  121.         shift;
  122.         ;;
  123.     -- ) shift; break;;
  124.     * ) break;;
  125.     esac
  126. done
  127.  
  128. case "$operation" in
  129.  
  130. # an eixe zitithei firstnames
  131.     fnames)
  132.  
  133. #ektupwnei ta firstnames alfavitika
  134.         awk 'BEGIN{FS="|"} /^[^#]/ {print $3|"sort -u"}' $file
  135.         ;;
  136.  
  137. # an eixe zitithei lastnames
  138.     lnames)
  139.  
  140. #ektupwnei ta lastnames alfavitika
  141.         awk 'BEGIN{FS="|"} /^[^#]/ {print $2|"sort -u"}' $file
  142.         ;;
  143.  
  144.     social) ##TODO gia kapoio logo sto dates.dat de douleuei swsta omws otan to ekana copy kai to onomasa random.dat doulepse idk
  145.         awk 'BEGIN{FS="|";}{sub("\r$", "");} /^[^#]/ {print $9}' $file | sort | uniq -c | awk  ' {print $2,$1}'
  146.         ;;
  147.  
  148. # an eixe zitithei kapoia imerominia
  149.     born)
  150.  
  151. # an exei dwthei mono born-until (to bornsince tha einai 0 giati den dwthike)
  152.         if (( "$bornsince"==0 ))
  153.         then
  154.  
  155. # ektupwse ola ta stoixeia twn atomwn me imerominia genisis<bornuntil
  156.             awk -v bornuntil="$bornuntil" 'BEGIN{FS="|"} /^[^#]/ && $5<=bornuntil { print $0 }' $file
  157.  
  158. #an exei dwthei mono born-since (to bornuntil tha einai 0 giati den dwthike)
  159.         elif (( "$bornuntil"==0 ))
  160.         then
  161.  
  162. # ektupwse ola ta stoixeia twn atomwn me imerominia genisis>bornsince
  163.             awk -v bornsince="$bornsince" 'BEGIN{FS="|"} /^[^#]/ && $5>=bornsince { print $0 }' $file
  164.  
  165. #an exoun dwthei kai bornsince kai bornuntil
  166.         else
  167.  
  168. # ektupwse ta stoixeia twn atomwn me bornsince<imerominia genisis< bornuntil
  169.             awk -v bornsince="$bornsince" -v bornuntil="$bornuntil" 'BEGIN{FS="|"} $5>=bornsince && $5<=bornuntil { print $0 }' $file
  170.         fi
  171.         ;;
  172. esac
Add Comment
Please, Sign In to add comment