Basic bash concepts VII Text Processing Hackerrank

1. Basic bash hackerrank
2. Shell beginner
3. Bash II chapter
4.Bash III chapter
5.Bash IV chapter
6.Bash V chapter
7.Bash VI chapter

https://coderinme.com/basic-bash-conce…ssing-hackerrank/

What is Paste command used in linux/unix

The paste command displays the corresponding lines of multiple files side-by-side.
It merges the lines from multiple files. The paste command sequentially writes the corresponding lines from each file separated by a TAB delimiter on the unix terminal.

The syntax is

paste [options] files-list

> cat fileA
Unix
Linux
Windows

> cat fileB
Dedicated server
Virtual server

> cat fileC
Hosting
VMachine
Operating system

1.By default Merging files in parallel

> paste fileA fileB
Unix    Dedicated server
Linux   Virtual server
Windows

> paste fileB fileA
Dedicated server  Unix
Virtual server    Linux
                  Windows

2.Using Delimiter

paste -d"|" fileA fileB
Unix|Dedicated server
Linux|Virtual server
Windows|

 paste -d":" fileA fileB
Unix:Dedicated server
Linux:Virtual server
Windows:

3.Merging files in sequentially.

paste -s file1 file2
Unix    Linux   Windows
Dedicated server        Virtual server

4. multiple delimiter and merging -s -d

paste -d"|," fileA fileB fileC
Unix|Dedicated server,Hosting
Linux|Virtual server,VMachine
Windows|,Operating system
paste -s -d"," fileA fileB
Unix,Linux,Windows
Dedicated server,Virtual server

5. we can merge multiple lines

cat fileA | paste - -
Unix    Linux
Windows

now let’s practice

Basic bash concepts VII Text Processing Hackerrank

Q1.Task
You are given a CSV file where each row contains the name of a city and its state separated by a comma. Your task is to replace the newlines in the file with semicolons as demonstrated in the sample.

Input Format

You are given a CSV file where each row contains the name of a city and its state separated by a comma.

Output Format

Replace the newlines in the input file with semicolons as demonstrated in the sample.

Sample Input

Albany, N.Y.
Albuquerque, N.M.
Anchorage, Alaska
Asheville, N.C.
Atlanta, Ga.
Atlantic City, N.J.
Austin, Texas
Baltimore, Md.
Baton Rouge, La.
Billings, Mont.
Birmingham, Ala.
Bismarck, N.D.
Boise, Idaho
Boston, Mass.
Bridgeport, Conn.

Sample Output

Albany, N.Y.;Albuquerque, N.M.;Anchorage, Alaska;Asheville, N.C.;Atlanta, Ga.;Atlantic City, N.J.;Austin, Texas;Baltimore, Md.;Baton Rouge, La.;Billings, Mont.;Birmingham, Ala.;Bismarck, N.D.;Boise, Idaho;Boston, Mass.;Bridgeport, Conn.

Explanation

The delimiter between consecutive rows of data has been transformed from the newline to a semicolon.

paste -s -d';'

here -s is used for merging the lines. and =d for the delimiter.

a=`tr '\n' ';'`
echo "${a::-1}"

or

tr '\n' ';'| sed -r 's/;$//g'

Q2.Task

You are given a CSV file where each row contains the name of a city and its state separated by a comma.


Sample Input

Albany, N.Y.
Albuquerque, N.M.
Anchorage, Alaska
Asheville, N.C.
Atlanta, Ga.
Atlantic City, N.J.
Austin, Texas
Baltimore, Md.
Baton Rouge, La.
Billings, Mont.
Birmingham, Ala.
Bismarck, N.D.
Boise, Idaho
Boston, Mass.
Bridgeport, Conn

Sample Output

Albany, N.Y.;Albuquerque, N.M.;Anchorage, Alaska
Asheville, N.C.;Atlanta, Ga.;Atlantic City, N.J.
Austin, Texas;Baltimore, Md.;Baton Rouge, La.
Billings, Mont.;Birmingham, Ala.;Bismarck, N.D.
Boise, Idaho;Boston, Mass.;Bridgeport, Conn.

Explanation

The given input file has been reshaped as required.

paste -d';' - - -

three – for 3 consecutive lines. -d for delimiter.

Q3.Replace the newlines in the input with tabs as demonstrated in the sample.

paste -s

Q4.Restructure the file in such a way, that every group of three consecutive rows are folded into one, and separated by tab.
Sample Input

Albany, N.Y.
Albuquerque, N.M.
Anchorage, Alaska
Asheville, N.C.
Atlanta, Ga.
Atlantic City, N.J.
Austin, Texas
Baltimore, Md.
Baton Rouge, La.
Billings, Mont.
Birmingham, Ala.
Bismarck, N.D.
Boise, Idaho
Boston, Mass.
Bridgeport, Conn.
Sample Output

Albany, N.Y. Albuquerque, N.M. Anchorage, Alaska
Asheville, N.C. Atlanta, Ga. Atlantic City, N.J.
Austin, Texas Baltimore, Md. Baton Rouge, La.
Billings, Mont. Birmingham, Ala. Bismarck, N.D.
Boise, Idaho Boston, Mass. Bridgeport, Conn.

paste - - -

or
paste -d'\t' - - -



anothre solution paste -d ''| paste - - -

 

Declaration

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.