Example for text processing Shell commands – tr, uniq, sort, sed and awk



Desk of Contents




Introduction

  • Textual content processing in shell script is all the time simple and efficient utilizing varied instructions.
  • On this weblog, we’ll choose a pattern sentence and see using instructions tr, uniq, kind, sed and awk to course of the sentence
  • These instructions are very efficient and has consumer pleasant choices to course of and print string as per consumer comfort
  • In real-time utilization – The textual content processing shall be wanted in case if we have to course of a bigger log recordsdata, and print the required values as per consumer comfort



Goal

For the given string, we have to depend the variety of occurrences of alphanumeric (strings) and print output to the console (in descending order) as per the format given under,

Instance:

string1->4
string2->3
string3->3
string4->2
Enter fullscreen mode

Exit fullscreen mode




Instructions intro

  • tr – Interprets the given string within the specified sample
  • uniq – Omits repeated string occurrences
  • sed – Textual content stream editor, one of many highly effective sh command for filtering/remodeling texts
  • kind – Type the strains of string or recordsdata
  • awk – Sample matching and textual content processing command, it’s one other highly effective command like sed



Allow us to create the command

  • Retailer the given string in a variable
$ string="That is the Pattern sentence, that incorporates repeated pattern string exists greater than as soon as within the pattern sentence. Repeat as soon as extra added within the pattern string"
Enter fullscreen mode

Exit fullscreen mode

  • Right here is the command to realize our goal
$ echo $string | tr -c '[:alnum:]' 'n' | tr '[:upper:]' '[:lower:]' | sed '/^$/d' | kind | uniq -c | kind -nr | awk '{ print $2"->"$1 }'
Enter fullscreen mode

Exit fullscreen mode

  • Executing the command will print the output in console, as per the anticipated format
$ echo $string | tr -c '[:alnum:]' 'n' | tr '[:upper:]' '[:lower:]' | sed '/^$/d' | kind | uniq -c | kind -nr | awk '{ print $2"->"$1 }'
sample->4
the->3
string->2
sentence->2
once->2
more->2
in->2
this->1
that->1
than->1
repeated->1
repeat->1
is->1
exists->1
contains->1
added->1
Enter fullscreen mode

Exit fullscreen mode



Allow us to breakdown the instructions

  • tr -c '[:alnum:]' 'n' -> Convert the paragraph (all alphanumeric chars) into one column single phrase per line
  • tr '[:upper:]' '[:lower:]' -> Convert higher case letters to decrease case letters
  • sed '/^$/d' -> Take away empty newlines
  • kind -> Type every line alphabetically
  • uniq -c -> Depend the phrase occurrences and prefix strains by the variety of occurrences
  • kind -nr -> Examine and kind in response to string numerical worth and print in reverse desc order
  • awk '{ print $2"->"$1 }' -> Print the kind output as per the anticipated format



Conclusion

  • On this weblog, we mentioned among the textual content processing shell instructions and printed a pattern output as per the anticipated format
  • As a reader, you may discover the choices of every instructions from the man pages and study extra about it and apply when wanted

Thanks for studying!



References

Man Pages




Comply with me on,

Add a Comment

Your email address will not be published. Required fields are marked *