SalmaAlassal / Hackerrank-LinuxShell

My solutions for linux shell.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Linux Shell

Text Processing

Challenge Solution
Head of a Text File #1 head -n 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 -n 20
Tail of a Text File #2 tail -c 20
Paste - 1 paste -sd ';'
Paste - 2 paste -d ';' - - -
Paste - 3 paste -sd "\t"
Paste - 4 paste -d '\t' - - -
Cut #1 cut -c 3
Cut #2 cut -c 2,7
Cut #3 cut -c 2-7
Cut #4 cut -c 1-4
Cut #5 cut -f 1-3
Cut #6 cut -c 13-
Cut #7 cut -f 4 -d " "
Cut #8 cut -f 1-3 -d " "
Cut #9 cut -f 2-
Sort Command #1 sort
Sort Command #2 sort -r
Sort Command #3 sort -n
Sort Command #4 sort -nr
Sort Command #5 sort -t $'\t' -nr -k 2
Sort Command #6 sort -nt $'\t' -k 2
Sort Command #7 sort -nrt $'|' -k 2
'Uniq' Command #1 uniq
'Uniq' Command #2 uniq -c | cut -c 7-
'Uniq' Command #3 uniq -ci | cut -c 7-
'Uniq' Command #4 uniq -u
'Tr' Command #1 tr "()" "[]"
'Tr' Command #2 tr -d "a-z"
'Tr' Command #3 tr -s ' ' ' '

Grep Sed Awk

Challenge Solution
'Grep' #1 grep -w "the"
Or
grep "\bthe\b"
'Grep' #2 grep -iw "the"
'Grep' #3 grep -ivw "that"
'Grep' - A grep -iwE "the|that|then|those"
Or
grep -iw -e "the" -e "that" -e "then" -e "those"
Or
grep -iE "\bthe\b|\bthat\b|\bthen\b\bthose\b"
'Grep' - B grep '\([0-9]\) *\1'
'Sed' command #1 sed 's/\bthe\b/this/'
'Sed' command #2 sed 's/thy/your/gi'
'Sed' command #3 sed 's/thy/{&}/gi'
'Sed' command #4 sed 's/[0-9]\+ /**** /g'
'Sed' command #5 sed -E 's/(.... )(.... )(.... )(....)/\4 \3\2\1/'
'Awk' - 1 awk '{if (NF != 4) print "Not all scores are available for "$1}'
Or
awk '{if (NF < 4){print "Not all scores are available for "$1}}'
'Awk' - 2 awk '{if ($2 < 50 || $3 < 50 || $4 < 50){print $1,": Fail"}else {print $1,": Pass"}}'
Or
awk '{print $1,":", ($2<50||$3<50||$4<50) ? "Fail" : "Pass"}'
Awk' - 3 awk '{avg=($2+$3+$4)/3; print $0, ":", (avg<50)?"FAIL":(avg<80)?"B":"A"}'
'Awk' - 4 awk 'ORS = NR%2? ";" : "\n" '

Bash

Challenge Solution File
Let's Echo echo "HELLO"
The World of Numbers read a
read b
let sum=$a+$b
let difference=$a-$b
let product=$a*$b
let quotient=$a/$b
echo -e "$sum\n$difference\n$product\n$quotient"
More on Conditionals read x
read y
read z
if ([ "$x" = "$y" ] && [ "$x" = "$z" ] && [ "$y" = "$z" ]) ; then
echo "EQUILATERAL"
elif ( [ "$x" = "$y" ] || [ "$x" = "$z" ] || [ "$y" = "$z" ] ); then
echo "ISOSCELES"
else
echo "SCALENE"
fi
Looping and Skipping for i in {1..99}; do
if [ $(($i%2)) -ne "0" ]; then
echo "$i"
fi
done
Looping with Numbers for i in {1..50}; do
echo "$i"
done
Getting started with conditionals read var
if [ "$var" == "y" -o "$var" == "Y" ] ; then
echo "YES"
else
echo "NO"
fi
Compute the Average read n
for i in $(seq 1 $n); do
read x
((sum+=$x))
done
avg=$(echo "$sum /$n"|bc -l)
printf "%.3f \n" $avg
Arithmetic Operations read input;
res=$(echo "$input" | bc -l)
printf "%.3f \n" $res
A Personalized Echo read name
echo "Welcome $name"
Comparing Numbers read x
read y
if [ $x -lt $y ]; then
echo "X is less than Y"
elif [ $x -gt $y ]; then
echo "X is greater than Y"
else
echo "X is equal to Y"
fi

or

read x
read y
if (( $x < $y )); then
echo "X is less than Y"
elif (( $x > $y )); then
echo "X is greater than Y"
else
echo "X is equal to Y"
fi


About

My solutions for linux shell.


Languages

Language:Shell 100.0%