Skip to content

Instantly share code, notes, and snippets.

@orymate
Last active April 2, 2021 20:40
Show Gist options
  • Save orymate/5444032 to your computer and use it in GitHub Desktop.
Save orymate/5444032 to your computer and use it in GitHub Desktop.
UNIX gyakorló példák
#!/bin/bash
# display line-of-comments/loc rate of parameters
LOC=0
EMPTY=0
COMMENT=0
printf "%-40s%8s%8s%8s\n" "File name" "LOC" "Comment" "SLOC"
while [ "$1" != '' ]
do
this_loc=$(wc -l < "$1")
this_comment=$(awk '/\/\*/, /\*\// {c++;next}
/\/\// {c++} END {print c}' "$1")
this_empty=$(grep -c '^\s*$' "$1")
this_sloc=$(($this_loc-$this_comment-$this_empty))
printf "%-40s%8d%8d%8d\n" "$1" $this_loc $this_comment $this_sloc
let LOC+=$this_loc
let EMPTY+=$this_empty
let COMMENT+=$this_comment
shift
done
PERC=$(($COMMENT*100/($LOC+1)))
printf "%-40s%8d%8d%8d (%d%% comment)\n" "SUM" $LOC $COMMENT $(($LOC-$COMMENT-$EMPTY)) $PERC
#!/bin/sh
# display numbers (one per line) as ranges
sort -n | awk '
BEGIN { last = "x" }
/^[0-9]+$/ {
if (last == "x") {
range = $1
}
else if (last + 1 != int($1)) {
if (range != last){
printf "%d-%d\n", range, last
range = int($1)
}
else {
printf "%d\n", last
range = int($1)
}
}
last = $1
}
END {
if (range != last){
printf "%d-%d\n", range, last
}
else {
printf "%d\n", last
}
}'
# abort scp if no remote path given
scp () {
for i in $*
do case $i in
*:*) command scp $*
return $?
esac
done
echo No remote path parameter. Aborting.
return 1
}
# change to temp dir
t() {
if [ ${1:-0} -eq 0 ]
then
mkdir -p ~/temp/$(date -d "${1:-0} days ago" +%Y%m%d)
fi
cd ~/temp/$(date -d "${1:-0} days ago" +%Y%m%d)
}
# change to previous directory
p() {
cur=$(basename "$PWD")
cd $(dirname "$PWD")/$(ls $(dirname "$PWD") -a |grep -EB1 "^${cur}/?$"|head -1)
}
# change to next directory
n() {
cur=$(basename "$PWD")
cd $(dirname "$PWD")/$(ls $(dirname "$PWD") -a |grep -EA1 "^${cur}/?$"|tail -1)
}
#!/usr/bin/awk -f
# transpose a semicolon-separated-variables file
BEGIN {
FS=";"
}
{
a[i++] = $0
max = max > NF ? max : NF
}
END {
for (i = 1; i <= max; i++) {
for (j in a) {
$0 = a[j]
printf "%s%s", $i, FS
}
print ""
}
}
#!/bin/bash
# Convert $1 decimal number to base $2 [2..10]
c ()
{
[ $1 -ge $2 ] && echo -n $(c $(expr $1 / $2) $2)
echo -n $(expr $1 % $2)
}
if [ $# -ne 2 ]
then
echo Usage: $0 NUMBER BASE
exit 1
fi
c $1 $2
echo
#!/bin/bash
# Magyar XKCD jelszó-generátor http://xkcd.com/936/
DB=/var/tmp/frequent-words
CORPUS='ftp://ftp.mokk.bme.hu/Language/Hungarian/Freq/Web2.2/web2.2-freq-sorted.top100k.nofreqs.txt'
if [ ! -e "$DB" ]
then
wget "$CORPUS" -O - |
iconv -f latin2 | hunspell -G | hunspell -s |
iconv -t ascii//TRANSLIT | tr -d ":'\"" |
awk '$2 ~ /^[:lower:]/{print $2}' | sort -u > /var/tmp/frequent-words
fi
for i in $(seq ${2:-1})
do
echo $(sort -R "$DB" | head -n ${1:-4})
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment