Basic bash concepts V Text Processing Hackerrank

Before this post you have learnt
1. Basic bash hackerrank
2. Shell beginner
3. Bash II chapter
4.Bash III chapter
5.Bash IV chapter
In this post we will discuss about ‘tr’ and ‘sort’ command that is used for text processing.

What is tr command in Unix/Linux bash

tr is an UNIX utility command (Traverse & Replace ) for translating, or deleting, or squeezing repeated characters.
It is used for many purpose like : convert string from lower to upper or vice-versa.
repetitive task, deleting the character or completing the char set.

the syntax is

tr [op1] [op2]

or

tr -d 

tr -c

tr-C
tr-s

tr -t

the [] can contain
[:alnum:] all letters and digits
[:alpha:] all letters
[:blank:] all horizontal whitespace
[:cntrl:] all control characters
[:digit:] all digits
[:graph:] all printable characters, not including space
[:lower:] all lower case letters
[:print:] all printable characters, including space
[:punct:] all punctuation characters
[:space:] all horizontal or vertical whitespace
[:upper:] all upper case letters
[:xdigit:] all hexadecimal digits
[=CoderInMe=] all characters which are equivalent to CoderInMe

example

r '{}' '()' < inputfile > outputfile


 tr 'A-Z' 'a-z' < inpfile > outfile
 
 
 cat file7 | tr '[A-Z]' '[a-z]' > file8
 
 cat file7 | tr [:upper:] [:lower:] > file8

Basic bash concepts V Text Processing Hackerrank

Q1. In this challenge, we practice using the tr command because it is a useful translation tool in Linux.
For example:
‘e’ being transformed to ‘E’

$ echo "Hello" | tr "e" "E"

HEllo
Spaces being transformed to hyphens

$ echo "Hello how are you" | tr " " '-'

Hello-how-are-you
Digits (numerals) being deleted

$ echo "Hello how are you 1234" | tr -d [0-9]

Hello how are you

In a given fragment of text, replace all parentheses () with box brackets [] .

Sample Input

int i=(int)5.8
(23 + 5)*2
Sample Output

int i=[int]5.8
[23 + 5]*2

Solution

tr "()" "[]"


or

tr "(" "[" | tr ")" "]"

Q2.In a given fragment of text, delete all the lowercase characters a-z.
Sample Input

Hello
World
how are you
Sample Output

H
W

Solution
3 solutions for this challenge.


tr -d [:lower:]

tr -d a-z


tr -d abcdefghijklmnopqrstuvwxyz

you can do sed ‘s/[a-z]//g’

Q3.In a given fragment of text, replace all sequences of multiple spaces with just one space.


tr -s ' '

or
tr -s ' ' ' '

What is sort command

Sort command used to order/sort the elements or text. or sorting numerical values and strings. it also can order the lines in a text file.

The syntax is:

sort [options] filename

The options are:

-b : Ignores leading spaces in each line
-d : Uses dictionary sort order. only spaces and alphanumeric characters considering in sorting
-f : case sensitivity does not matter.
-M : Sorts based on months. like Eg: JAN, FEB
-n : Uses numeric 1 2 3
-R : Sorts the input file randomly.
-r : Reverse order sorting
-k : Sorts file based on the data in the specified field positions.
-u : Suppresses duplicate lines
-t : input field separator

if you want to check file is sorted?
sort -c filename

Q1.Given a text file, order the lines in lexicographical order.


sort
or
sort -n

Q2.Given a text file, order the lines in reverse lexicographical order (i.e. Z-A instead of A-Z).


sort -r

Q3.You are given a text file where each line contains a number(n<100). The numbers may be either an integer or have decimal places. There will be no extra characters other than the number or the newline at the end of each line. Sort the lines in ascending order - so that the first line holds the numerically smallest number, and the last line holds the numerically largest number.


sort -n

Q4.The ‘Sort’ Command Line Program
You are given a file of text, where each line contains a number (which may be either an integer or have decimal places). There will be no extra characters other than the number or the newline at the end of each line. Sort the lines in descending order – – such that the first line holds the (numerically) largest number and the last line holds the (numerically) smallest number.


sort -n -r

Q5.You are given a file of text,which contains temperature information about American cities, in TSV (tab-separated) format. The first column is the name of the city and the next four columns are the average temperature in the months of Jan, Feb, March and April (see the sample input). Rearrange the rows of the table in descending order of the values for the average temperature in January.


sort -n -r -t$'\t' -k2

try this sort -n -r -t$’\t’ -k2
where
-n : numeric sort
-r : reverse
-t : field separator
-k : sort via key

Q6.You are given a file of tab separated weather data (TSV). There is no header column in this data file.
The first five columns of this data are: (a) the name of the city (b) the average monthly temperature in Jan (in Fahreneit). (c) the average monthly temperature in April (in Fahreneit). (d) the average monthly temperature in July (in Fahreneit). (e) the average monthly temperature in October (in Fahreneit).

You need to sort this file in ascending order of the second column (i.e. the average monthly temperature in January).


sort -n -t$'\t' -k2

or sort -nt$’\t’ -k2

Q7.You are given a file of pipe-delimited weather data (TSV). There is no header column in this data file. The first five columns of this data are: (a) the name of the city (b) the average monthly temperature in Jan (in Fahreneit). (c) the average monthly temperature in April (in Fahreneit). (d) the average monthly temperature in July (in Fahreneit). (e) the average monthly temperature in October (in Fahreneit).

You need to sort this file in descending order of the second column (i.e. the average monthly temperature in January).


sort -r -n -t'|' -k2

or

sort -t'|' -n -k2,2 -r

continued………………

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.