Format columns and heads using shell script

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Format columns and heads using shell script
# 1  
Old 11-29-2016
Format columns and heads using shell script

My program output text file data in columns format. My problem is the column headers do not align with the data and if filesystem and Mounted On columns pathname are too long the text will not wrap.

Is there any function in Unix that I can use in my code that will align the data for each column with the correct headers? Is there any script in Unix that will wrap text if the pathname is too long?

1) The attachment named data not aligned with column headers shows the
current results.

2) The attachment named how i want the results. Is want I am looking for.

3) The attachment named disk_2016.11.29.txt is the file that is read in the code


here is my code
Code:
#!/bin/ksh
 
 
 
fpath=/sas/scripts/OPTUMize/output/disk_utilization
 
# get input
echo "Enter Group name (ex: hasasvmi): "
read gname
 
echo "Enter Current Date( ex: 10/01/2016): "
read cdate
# format: 09/30/2016
 
# parse date
yyyy=$(echo ${cdate} | cut -d"/" -f3)
dd=$(echo ${cdate} | cut -d"/" -f2)
mm=$(echo ${cdate} | cut -d"/" -f1)
 
echo "Enter number of records (ex: 1-1000): "
read nrec
 
# construct file name to parse
diskfile=${fpath}/disk_${yyyy}.${mm}.${dd}.txt
 
# check if file exists
if [ ! -f ${diskfile} ]
then
                echo File: ${diskfile} not found. Exiting.
                exit 1
fi
 
# generate output on screen - TAB separated (OFS)

awk -v gname=${gname} -v OFS="\t" 'BEGIN { print "Filesystem", "blocks", "Free", "% Used", "%Iused", "Mounted on", "Date", "Time" }
$0 ~ gname { gsub(/_/,""'"OFS"'"",$7); print $1, $2, $3, $4, $5, $6, $7 }' ${diskfile} | head -$(expr ${nrec} + 1)

# 2  
Old 11-29-2016
tl;dr the attachments but the printf command, which is implemented by both the shell and awk might be part of the solution of your issue.
# 3  
Old 11-29-2016
Hi.

I've used align for years on a number of platforms. Details are below.

To see demonstrations, search for align drl on this site ... cheers, drl
Code:
align   Align columns of text. (what)
Path    : ~/p/stm/common/scripts/align
Version : 1.7.0
Length  : 270 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Help    : probably available with --help
Home    : http://kinzler.com/me/align/
Modules : (for perl codes)
 Getopt::Std    1.10

# 4  
Old 11-29-2016
Format columns and heads using shell script

where would I place align or printf in my code?
# 5  
Old 11-30-2016
printf is a UNIX/Linux command - lives in /usr/bin/printf, on some systems it is part of shell language
The type command is your friend, example in bash shell:
Code:
$ type printf
printf is a shell builtin

align is perl code that drl suggests. perl runs under bash like awk does. You feed awk a series of awk syntax lines, it executes. perl is the same.


This shows you how to use align:
Aligning a file
It is a short script demo. You have to have the align perl code in your current working directory. It can also be in your PATH variable so the system can find it.

The script example also mentions where to download the perl code from
This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 11-30-2016
I added sprintf to my code and it doesn't appear to work. I am doing something wrong. I could not figure out how to use align in my code even with the demo.

Here is my code using sprintf. Please tell me what I did wrong.
Code:
!/bin/ksh
 
 
 
fpath=/sas/scripts/OPTUMize/output/disk_utilization
 
# get input
echo "Enter Group name (ex: hasasvmi): "
read gname
 
echo "Enter Current Date( ex: 10/01/2016): "
read cdate
# format: 09/30/2016
 
# parse date
yyyy=$(echo ${cdate} | cut -d"/" -f3)
dd=$(echo ${cdate} | cut -d"/" -f2)
mm=$(echo ${cdate} | cut -d"/" -f1)
 
echo "Enter number of records (ex: 1-1000): "
read nrec
 
# construct file name to parse
diskfile=${fpath}/disk_${yyyy}.${mm}.${dd}.txt
 
# check if file exists
if [ ! -f ${diskfile} ]
then
                echo File: ${diskfile} not found. Exiting.
                exit 1
fi
 
# generate output on screen - TAB separated (OFS)

awk -v gname=${gname} -v OFS="\t" 'BEGIN { print "Filesystem", "blocks", "Free", "% Used", "%Iused", "Mounted on", "Date", "Time" }
$0 ~ gname { gsub(/_/,""'"OFS"'"",$7); sprintf $1, $2, $3, $4, $5, $6, $7 }' ${diskfile} | head -$(expr ${nrec} + 1)

# 7  
Old 11-30-2016
Hi.
Quote:
Originally Posted by dellanicholson
... I could not figure out how to use align in my code even with the demo ...
You connect the output of one program with the input of another, like so:
Code:
your-program | ./align

which assumes you have the perl code align in the current directory.

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script change new format on the file.

Hi---Is there's way can write small shell script or perl script open "abc.txt" file and create new "new_abc.txt" file with format output below? Thanks cat abc.txt ###########################Readme############################### Contained with this README.TXT file are all of the file... (7 Replies)
Discussion started by: dotran
7 Replies

2. Shell Programming and Scripting

Format CSV file from a shell script

I have a shell script which creates a CSV files. there are 3 fields, field1, field2 and comp. I will generates the values for field1 and field2 and Want to compare both. If field1>filed2 then comp should be success written in green in CSV file, else it should fail in red color. How can I change the... (5 Replies)
Discussion started by: sauravrout
5 Replies

3. Shell Programming and Scripting

Change date format in shell script

Plz help me To display date in the mm/dd/yyyy. Eg. if date is 28-09-2012 the output of the shell script should be date 09/28/2012. (1 Reply)
Discussion started by: shivasaini
1 Replies

4. Shell Programming and Scripting

XML dateTime format in shell script

How can i frame current date & time in the xml standard dateTime format in shell script 2011-10-18T12:00:00.000000 I got up to the date format using the below code date '+%Y'-'%m'-'%d'T ---------- Post updated at 09:10 AM ---------- Previous update was at 08:53 AM ---------- ... (6 Replies)
Discussion started by: vel4ever
6 Replies

5. Shell Programming and Scripting

shell script | bc syntax format

sorry but need help http://i.investopedia.com/inv/articles/site/CalculationEMA.gif trying to achieve ema in script I have this syntax which errors ema=` ; the 0.153846154 ='s Smoothing Factor really appreciate help (3 Replies)
Discussion started by: harte
3 Replies

6. UNIX for Advanced & Expert Users

shell script to format .CSV data

Hi all, I have written a shell script to search a specified directory (e.g. /home/user) for a list of specific words (shown as ${TMPDIR}/wordlist below). The script works well enough, but I was wondering if there was a way to display the line number that the word is found on? Thanks! cat... (1 Reply)
Discussion started by: tmcmurtr
1 Replies

7. Solaris

format file using shell script

my question "format file using shell script " is not a homework. bad guess. my actual file is much more complex. the requirement is to format the file before i can read it from SAP. so i'd appreciate if any inputs can be provide. i've tried most of the commands like tr and sed and nawk, no... (2 Replies)
Discussion started by: balajim
2 Replies

8. Solaris

format file using shell script

Hi All, I am new to shell scripts. I have a requirement to change the format of a file. Here is the original file: #student layout student_name student_class student_subject david 5 chemistry paul 4 physics steve 6 mathematics This is the format i need: k1,david,5,chemistry... (1 Reply)
Discussion started by: balajim
1 Replies

9. Shell Programming and Scripting

Help writing shell script in c++ format

how would i write a shell script to count number of one-line comments in a c++ file. if anyone knows any good books or internet sites for shell script would be much apprecitied thanks (2 Replies)
Discussion started by: deadleg
2 Replies

10. Shell Programming and Scripting

Shell script to separte columns...

Hi all, I have a data file and need to load the data into tables in a database. The problem is I cannot use a while loop with each line as the data is free text and has all kinds of weird characters including carriage returns and new line characters. Each column is separated by ~^~ and a new line... (2 Replies)
Discussion started by: sam_78_nyc
2 Replies
Login or Register to Ask a Question