Table like formatting in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Table like formatting in Linux
# 1  
Old 03-31-2018
Table like formatting in Linux

Dear experts,

I need bit help in formatting ..

I have csv file file , i will read that file by passing one column value as input parameter and display header row and corresponding row for that parameter.

I have shell script like this:

Code:
#!/bin/bash
#key_word_I_am_looking_for=$1
#awk -F: -v keyword="$1" '$1 == keyword {$1=$1; print}' Book1.csv
#my_variable=`cat Book1.csv | awk -F: -v keyword="$key_word_I_am_looking_for" '( $1 == keyword )' END{print "$@" }'`
#echo "$my_variable"

SERVICE=$1
head -n 1 /opt/scripts/devv/book7.csv > message.out
awk '{gsub(/\,/,"       ");print;}' message.out
grep -i $1 /opt/scripts/devv/book7.csv > message1.out
awk '{gsub(/\,/,"          ");print;}' message1.out

I tried to use tab for header line..used same tab as delimiter for second line but second values not aligned with first line...can some one suggest please..

Current out put as below:
Code:
SERVICE NAME       TEAM NAME       CI OWNER       SECONDARY CONTACT       PAYCHECK NAME       DEVV       ADEINT       ENV DEV       ENV STAGE       ENV PROD       ENV PROD
checkout          SARD          opalan                        Jef May              abcdef             Yes          Yes          Yes          Yes          Yes          Yes

# 2  
Old 03-31-2018
If you want to properly format the output, then I suggest you use printf statement.
For example:
Code:
awk -F, '{printf "%13-s %10-s %9-s %18-s %14-s %5-s %7-s %8-s %10-s %9-s %9-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv

Note: the - sign should be behind the procent sign in the above example..
Code:
SERVICE NAME  TEAM NAME  CI OWNER  SECONDARY CONTACT  PAYCHECK NAME  DEVV  ADEINT  ENV DEV  ENV STAGE  ENV PROD  ENV PROD 
checkout      SARD       opalan    Jef May            abcdef         Yes   Yes     Yes      Yes        Yes       Yes

Or, combined with TAB's :
Code:
awk -F, '{printf "%12-s\t%9-s\t%8-s\t%17-s\t%13-s\t%4-s\t%6-s\t%7-s\t%9-s\t%8-s\t%8-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv

Note: the - sign should be behind the procent sign in the above example..

Code:
SERVICE NAME	TEAM NAME	CI OWNER	SECONDARY CONTACT	PAYCHECK NAME	DEVV	ADEINT	ENV DEV	ENV STAGE	ENV PROD	ENV PROD
checkout    	SARD     	opalan  	Jef May          	abcdef       	Yes 	Yes   	Yes    	Yes      	Yes     	Yes


Last edited by Scrutinizer; 04-01-2018 at 04:25 AM..
# 3  
Old 03-31-2018
Quote:
Originally Posted by Scrutinizer
If you want to properly format the output, then I suggest you use printf statement.
For example:
Code:
awk -F, '{printf "%13-s %10-s %9-s %18-s %14-s %5-s %7-s %8-s %10-s %9-s %9-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv

Code:
SERVICE NAME  TEAM NAME  CI OWNER  SECONDARY CONTACT  PAYCHECK NAME  DEVV  ADEINT  ENV DEV  ENV STAGE  ENV PROD  ENV PROD 
checkout      SARD       opalan    Jef May            abcdef         Yes   Yes     Yes      Yes        Yes       Yes

Or, combined with TAB's :
Code:
awk -F, '{printf "%12-s\t%9-s\t%8-s\t%17-s\t%13-s\t%4-s\t%6-s\t%7-s\t%9-s\t%8-s\t%8-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv

Code:
SERVICE NAME	TEAM NAME	CI OWNER	SECONDARY CONTACT	PAYCHECK NAME	DEVV	ADEINT	ENV DEV	ENV STAGE	ENV PROD	ENV PROD
checkout    	SARD     	opalan  	Jef May          	abcdef       	Yes 	Yes   	Yes    	Yes      	Yes     	Yes

Thanks for reply and suggestion..

but pardon me..i tried to implement it using your suggestion..but its displaying all records

changed by code as below. Is this correct ?

Code:
SERVICE=$1
head -n 1 /opt/scripts/devv/book7.csv
awk -F, '{printf "%12-s\t%9-s\t%8-s\t%17-s\t%13-s\t%4-s\t%6-s\t%7-s\t%9-s\t%8-s\t%8-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv
grep -i $1 /opt/scripts/devv/book7-1.csv
awk -F, '{printf "%12-s\t%9-s\t%8-s\t%17-s\t%13-s\t%4-s\t%6-s\t%7-s\t%9-s\t%8-s\t%8-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv

sorry if understanding is silly
# 4  
Old 03-31-2018
What do you need the SERVICE=$1 for? The SERVICE variable is not used anywhere in your code...

Try to adapt Scrutinizer's code (you were close in your commented out samples):
Code:
awk -F, -v keyword="$1" 'NR == 1 || $1 == keyword {printf "%12-s\t%9-s\t%8-s\t%17-s\t%13-s\t%4-s\t%6-s\t%7-s\t%9-s\t%8-s\t%8-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv

Check for the correct field separator.
Please be aware that $1 in shell differs from $1 in awk.
# 5  
Old 03-31-2018
Quote:
Originally Posted by RudiC
What do you need the SERVICE=$1 for? The SERVICE variable is not used anywhere in your code...

Try to adapt Scrutinizer's code (you were close in your commented out samples):
Code:
awk -F, -v keyword="$1" 'NR == 1 || $1 == keyword {printf "%12-s\t%9-s\t%8-s\t%17-s\t%13-s\t%4-s\t%6-s\t%7-s\t%9-s\t%8-s\t%8-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' /opt/scripts/devv/book7.csv

Check for the correct field separator.
Please be aware that $1 in shell differs from $1 in awk.
Dear RudiC,

Thanks for followup reply.

SERVICE=$1 is input parameter that i want to pass in while executing my shell script
and data for this parameter is available in book.7 csv file
and I shall run program as below
$asp > sh test7.sh checkout -- in this checkout one of value for columna called SERVICE in of that csv file

by passing this service name i should get relevant row from that csv and display it on screen both header and its record..


and now problem is with the formatting.. I want to display it strict tabular format, but header and child record are not aligning together properly

Here is some sample data in that csv: The challenge is actual data is vary in length ..the code that i am trying working (in the sense aligning correctly and for some its not aligning correctly. In true sense the problem Column header should adjust according to rows (i.e data rows) not vice versa.. which i am trying to achieve

Code:
SERVICE NAME	TEAM NAME	CI OWNER 	SECONDARY CONTACT	PAYCHECK NAME	DEVV	ADEINT	ENV DEV	  ENV STAGE	ENV PROD	    ENV PROD
accommodation,POSE,MatDon Richardson,Chrivarian,Acc,Yes,Yes,Yes,Yes,Yes,Yes
activitycodemappingtask,DLLARS,Ian,Patrick,Acc,Yes,Yes,Yes,Yes,Yes,Yes
activity-entity,Rega,Chang,John,TER,Yes,Yes,Yes,Yes,Yes,Yes
api-test-runner,WRIGHT,Benson,Lehman,Lev,Yes,Yes,No,No,No,No
assembly-service,Snap,Macdonald,Nikseth,Lev,Yes,Yes,Yes,No,No,Yes
authorizationprocessing,DLLARS,Ian,Pat,Acc,Yes,Yes,Yes,Yes,Yes,Yes

for example ; the below service having slight larger length paycheck name hence format is disturbed. you can see COLUMN Heading SECONADARY CONTACT NOT ALIGNED, PAYCHECK VALUE not aligned properly, However in CSV file data does not have any extra spaces it looks perfect like above sample data.
Code:
SERVICE NAME	TEAM NAME	CI OWNER 	SECONDARY CONTACT	PAYCHECK NAME	DEVV	ADEINT	ENV DEV	  ENV STAGE	ENV PROD	    ENV PROD
     cmc                   MULTIPLE     Timothy sutherland     Wills Robinson                              TER or LEV depending on plugin  Yes     Yes     Yes     Yes             Yes             Yes


Last edited by Scrutinizer; 03-31-2018 at 12:22 PM.. Reason: code tags again
# 6  
Old 03-31-2018
Try:
Code:
awk -F'\t *' '{printf "%25-s%16-s%20-s%26-s%16-s%8-s%10-s%14-s%10-s%10-s%-s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11} NR==1{FS=","}' file

Note: the - sign should be behind the procent sign in the above example..

Last edited by Scrutinizer; 04-01-2018 at 04:24 AM..
# 7  
Old 03-31-2018
Hi..

Thanks for reply..

now it better..but not completely

I see two problems now:

1. headers row not aligned
2. Most of data rows are aligned..i see only problem in those rows where SERVICE column data is having huge length that why its pushing other columns..the max length of this column is 40....

after position 40 the rest of the columns to be printed .. how can i force to print 2nd column onwards after 40th position
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Web Development

Getting Rid of Annoying Bootstrap Table Borders and Wayward Table Lines

Bootstrap is great; but we have had some issues with Bootstrapped <tables> (and legacy <fieldset> elements) showing annoying, wayward lines. I solved that problem today with this simple jQuery in the footer: <script> $(function(){ $('tr, td, fieldset,... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

Formatting in a table via shell

Hi I have the below output: 736f14c4-eda2-4531-9d40-9de4d6d1fb0f An account already exists for this email address. Please enter a different email address. c3716baf-9bf8-42da-8a44-a13fff68d20f An account already exists for this email address. Please enter a different email address. Is... (4 Replies)
Discussion started by: ankur328
4 Replies

3. Shell Programming and Scripting

Build a table from a list by comparing existing table entries

I am new to this shell scripting.... I have a file which contains list of users. This files get updated when new user comes into the system. I want to create script which will give a table containing unique list of users. When I say unique, it means script should match table while parsing... (3 Replies)
Discussion started by: dchavan1901
3 Replies

4. Red Hat

Parsing a linux file and formatting it.

Hi, I have a linux file that has data like this.. REQUEST_ID|text^Ctext^Ctext^C REQUEST_ID|text^Ctext^C REQUEST_ID| REQUEST_ID| REQUEST_ID|text^Ctext^Ctext^Ctext^Ctext^Ctext^C.... Where ever I see a ^C character, I need to copy the corresponding REQUEST_ID and that part of the text to a new... (17 Replies)
Discussion started by: charithainfadev
17 Replies

5. Solaris

Linux partitioned disk mounted on OSOL without formatting

Hello and Merry Christmas... Quick question after tireless search around the web. Description: I have a WD My book world edition II that met an untimely death. However the 2 SATA disks inside seem to be working just fine. Want to add either one of them to my Solaris Desktop. Since I... (5 Replies)
Discussion started by: michnmi
5 Replies

6. Shell Programming and Scripting

Insert into Oracle table thru UNIX - linux 2.6.9-89

Hi, I am trying to insert a record into a table (say dips_tbl) which resides in Oracle DB through a ksh script. I want to insert records into few of the table columns-not all. I'll give an e.g. for the date column "CREATE_DATE". For that I first execute SQL1="SELECT SYSDATE FROM DUAL" ... (1 Reply)
Discussion started by: dips_ag
1 Replies

7. Shell Programming and Scripting

select values from db1 table and insert into table of DB2

Hi I am having three oracle databases running in three different machine. their ip address is different. from one of the DB am able to access both the databases.(means am able to select values and insert values in to tables individually.) I need to fetch some data from DB1 table(say DB1 ip is... (2 Replies)
Discussion started by: aemunathan
2 Replies

8. Shell Programming and Scripting

help on formatting output (Table Form)

Data in File ABC:DEFGHI:123 ABCZYE:DEFI:123 ABCFGD:DEF:123 ABCEERRRRR:DEFGHI:123 Expected Format 1 ABC DEFGHIFE 123 2 ABCZYE DEFI 123 3 ABCFGD DEF 123 4 ABCEERRRRR DEFGHI 123 However when i enter the following... (2 Replies)
Discussion started by: blurboy
2 Replies

9. Shell Programming and Scripting

How to obtain system open file table value in Linux

Hello , I want to get current system open file table value. Can any one help. Thanking you, mahesh (0 Replies)
Discussion started by: mahesh.
0 Replies
Login or Register to Ask a Question