HACKERRANK LINUX SHELL
BASH
Let's Echo
echo "HELLO"
Looping and Skipping
for i in {1..50}
do
echo $i
done
A Personalized Echo
read nombre
echo "Welcome $nombre"
Looping with Numbers
for i in {1..50}
do
echo $i
done
The World of Numbers
read X
read Y
echo $((X+Y))
echo $((X-Y))
echo $((X*Y))
echo $((X/Y))
Comparing Numbers
read X
read Y
if (( $X > $Y ))
then
echo "X is greater than Y"
fi
if (( $X == $Y))
then
echo "X is equal to Y"
fi
if(( $X < $Y))
then
echo "X is less than Y"
fi
Getting started with conditionals
read palabra
if [[($palabra == 'y') || ($palabra == 'Y')]]
then
echo "YES"
elif [[($palabra == 'n') || ($palabra == 'N')]]
then
echo "NO"
fi
More on Conditionals
read x
read y
read z
if ((($x == $y) && ($y == $z)))
then
echo "EQUILATERAL"
elif ((($x == $y) || ($x == $z) || ($y == $z)))
then
echo "ISOSCELES"
else
echo "SCALENE"
fi
Arithmetic Operations
read x
printf "%.3f\n" `echo "$x" | bc -l`
Compute the average
read N
sum=0
for ((i=0; i<N; i++)); do
read num
sum=$((sum + num))
done
avg=$(echo "scale=4; $sum / $N" | bc)
printf "%0.3f" $avg
Functions and Fractals
declare -A a
f() {
local d=$1 l=$2 r=$3 c=$4
[[ $d -eq 0 ]] && return
for ((i=l; i; i--)); do
a[$((r-i)).$c]=1
done
((r -= l))
for ((i=l; i; i--)); do
a[$((r-i)).$((c-i))]=1
a[$((r-i)).$((c+i))]=1
done
f $((d-1)) $((l/2)) $((r-l)) $((c-l))
f $((d-1)) $((l/2)) $((r-l)) $((c+l))
}
read n
f $n 16 63 49
for ((i=0; i<63; i++)); do
for ((j=0; j<100; j++)); do
if [[ ${a[$i.$j]} ]]; then
printf 1
else
printf _
fi
done
echo
done
ARRAY IN BASH
Read in an Array
while read linea
do
arr=(${arr[@]} $linea)
done
echo ${arr[@]}
Slice an Array
arr=($(cat))
echo ${arr[@]:3:5}
Filter an Array with Patterns
arr=($(cat))
echo ${arr[@]/*[aA]*/}
Concatenate an array with itself
arr=($(cat))
arr=("${arr[@]}" "${arr[@]}" "${arr[@]}")
echo ${arr[@]}
Display an element of an array
arr=($(cat))
echo ${arr[3]}
Count the number of elements in an Array
arr=($(cat))
echo ${#arr[@]}
Remove the First Capital Letter from Each Element
arr=($(cat))
echo ${arr[@]/[A-Z]/.}
Lonely Integer - Bash!
read
arr=($(cat))
echo "${arr[@]}" | tr ' ' '\n' |sort | uniq -u | tr '\n' ' '
TEXT PROCESSING
Cut #1
cut -c 3
Cut #2
cut -c 2,7
Cut #3
cut -c 2-7
Cut #4
cut -c -4
Cut #5
cut -f 1-3
Cut #6
cut -c 13-
Cut #7
cut -d " " -f 4
Cut #8
cut -d " " -f 1-3
Cut #9
cut -f 2-
Head of a Text File #1
head -20
Head of a Text File #2
head -c 20
Middle of a Text File
head -22 | tail -11
Tail of a Text File #1
tail -20
Tail of a Text File #2
tail -c 20
'Tr' Command #1
tr "()" "[]"
'Tr' Command #2
tr -d "a-z"
'Tr' Command #3
tr -s ' '
Sort Command #1
sort
Sort Command #2
sort -r
Sort Command #3
sort -n
Sort Command #4
sort -n -r
Sort Command #5
sort -k2 -n -r -t$'\t'
Sort Command #6
sort -n -k2 -t$'\t'
Sort Command #7
sort -k2 -n -r -t '|'
'Uniq' Command #1
uniq
'Uniq' Command #2
uniq -c | cut -c7-
'Uniq' Command #3
uniq -i -c | cut -c7-
'Uniq' Command #4
uniq -u
Paste – 1
paste -d";" -s
Paste – 2
paste -d ";" - - -
Paste – 3
paste -s
Paste – 4
paste - - -
GREP SED AWK
'Awk' – 1
awk '{ if(length($4) == 0) print "Not all scores are available for " $1 }'
'Awk' – 2
awk '{ print $1 ($2 < 50 || $3 < 50 || $4 < 50 ? " : Fail" : " : Pass") }'
'Awk' – 3
awk '{ total=$2+$3+$4; avg=total/3; print $0 " : " (avg > 50 ? avg > 60 ? avg > 80 ? "A" : "B"
: "C" : "FAIL"); }'
'Awk' – 4
awk 'END{ if((NR%2)) print p ";" }!(NR%2){ print p ";" $0 }{ p = $0 }'
'Grep' #1
grep -w "the"
'Grep' #2
grep -iw "the"
'Grep' #3
grep -iv "that"
'Grep' -A
grep -iwe "the\|that\|then\|those"
'Grep' -B
grep '\([0-9]\) *\1'
'Sed' command #1
sed -e 's/the /this /1'
'Sed' command #2
sed -e 's/thy /your /gI'
'Sed' command #3
sed -e 's/thy/{&}/gI'
'Sed' command #4
sed -r 's/[0-9]{4}[ ]/**** /g'
'Sed' command #5
sed -E 's/([0-9]{4}) ([0-9]{4}) ([0-9]{4}) ([0-9]{4})/\4 \3 \2 \1 /g'