String Manipulations in Bash

Length

We can access the length of a string using the hash (#) operator.

$ VAR=Batur
$ echo ${#VAR}

Substrings

We can extract a substring using the colon (:) operator.

$ VAR=Batur
$ echo ${VAR:1}
$ echo ${VAR:2}
$ echo ${VAR:1:3}

Substring Match

Following syntax deletes or replaces match of $substring from $string

${string##sub}
$ VAR="Batur Orkun"
$ echo ${VAR##Batur}
${string%%sub}
$ VAR="Batur Orkun"
$ echo ${VAR%%Orkun}
${string/pattern/replacement}
$ VAR="Batur Orkun"
$ echo ${VAR/r/R}
${string//pattern/replacement}
$ VAR="Batur Orkun"
$ echo ${VAR/r/R}
digit="456"if [[ $digit =~ [0-9] ]]; then
echo "$digit is a digit"
else
echo "digit is NO digit "
fi
digit="456a"if [[ $digit =~ ^-?[0-9]+$ ]]; then
echo "$digit is a digit"
else
echo "digit is NO digit "
fi
  • The - is a literal "-"
  • The ? means "0 or 1 of the preceding (-)"
  • The + means "1 or more of the preceding ([0-9])"
  • The $ indicates the end of the input pattern

grep = global regular expression print

It is unargued that the most popular and used. It uses to search a string in the output of a command and in a file or files.

$ grep "batur" myfile
$ grep -n "batur" myfile
$ grep -l "batur" myfile
  • “ -l “ : Print only the names of files with matching lines, separated by newline characters.
  • “ -i “ : Ignore upper/lower case distinction during comparisons.
  • “ -n “ : Print each line by its line number in the file. ( first line is 1).
  • “ -v “: Print all lines except those that contain the pattern.
  • “ -r “: It recursively searches the pattern in all the files in the current directory and all its sub-directories.
  • “ -w “: It searches the exact word
$ grep -i "batur" myfile
$ grep "Orkun$" myfile
$ grep "^Batur Orkun$" myfile
$ pgrep nginx

sed: stream editor

SED performs editing operations on text coming from standard input or a file. It can do lots of functions on files like, searching, find and replace, insertion or deletion. SED supports regular expression which allows it to perform complex pattern matching.

$ sed 's/devops/DevOps/' myfile
$ sed "s/devops/DevOps/" myfile > newfile
$ sed -ni "s/devops/DevOps/" myfile

“s” : substitute

The “s” command is probably the most important in sed and has a lot of different options. Its basic concept is simple: the s command attempts to match the pattern space against the supplied regexp. if the match is successful, then that portion of the pattern space which was matched is replaced with replacement. We used it above.

Syntax: "s/regexp/replacement/flags"

“p”: Print

Print out the pattern space (to the standard output).

Syntax: "/pattern/ command"
$ ls -l | sed -n '/picus/ p'
$ ls -l | sed -n '/picus/ p' | grep '^d'
$ sed -i '/picus/ d' myfile

“d”: Delete command

Delete the pattern space

AWK

A text pattern scanning and processing language. Yes, You read right! AWK is a text-processing programming language. It is a direct predecessor of PERL and is still very useful in modern systems.

  • Scans a file line by line.
  • Splits each input line into fields.
  • Compares input line/fields to a pattern.
  • Performs actions on matched lines.
  • Produce formatted reports.
  • Conditionals and loops.
BEGIN { …. initialization awk commands …}
{ …. man awk commands …}
END { …. finalization awk commands …}
$ ls -l | awk 'BEGIN {sum=0} {sum=sum+$5} END {print sum}'
$ echo "A-B-C-D-E" | awk -F "-" '{ print $2 }'
$ echo "A-B-C-D-E" | awk -F "-" '{ print $1,$5 }'
1) Name        Surname   City 
2) Batur Orkun Ankara 70
3) HaticeEbru Orkun Istanbul 95
$ awk '{print $2 “=” $4}' myfile
$ awk '/Orkun/ {print $0}' myfile
$ awk '/Orkun/{++cnt} END {print “Count = “, cnt}' myfile
$ awk 'length($2) > 5' myfile
$awk '{if ($5>70) print}' myfile
$ awk '{ print substr( $1, 0,1 ) }' myfile
$ awk '{if(NR>1)print}' myfile
$ awk '{if (NF==4) print}' myfile

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store