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
# 8  
Old 11-30-2016
Format columns and heads using shell script

The OS is AIX

I tried using your example
Code:
grpdsku.sh | ./align

but I get an error message
this is no pipe to read data written to a pipe.

I tried using printf in my code but the headers and columns format disappeared.


I get an error message when I used sprintf in my code:

Code:
awk -v gname=${gname} -v OFS="\t" 'BEGIN { sprintf "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)

Code:
grpdsku.sh | ./align

when I use printf in my code, the headers and columns format disappear:

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

# 9  
Old 11-30-2016
Hi.

For a system like:
Code:
aix 7.1.0.0
bash GNU bash 4.2.10
align 1.7.0
perl 5.10.1

Code:
$ echo -e "hi world\nbye cruel" | ./align

produces
Code:
hi  world
bye cruel

Best wishes ... cheers, drl
# 10  
Old 11-30-2016
awk's sprintf is used to supply formatted strings or (partial) lines of text for variable assignments or printouts, not for printing itself.
Both (s)printf versions need a format string as the first parameter which you fail to provide in your samples.
# 11  
Old 11-30-2016
I tried many ways but nothing appears to work
# 12  
Old 11-30-2016
Without seeing the "many ways" we can't pin the points of failure in either, I'm afraid.


EDIT:
Howsoever, what about sth along this line:
Code:
awk -v gname=hasasvmi -v OFS="\t" '
BEGIN           {FMT = "%-20s\t%6s\t%4s\t%6s\t%6s\t%16s\t%10s\t%8s\n"
                 printf FMT, "Filesystem", "blocks", "Free", "% Used", "%Iused", "Mounted on", "Date", "Time"
                }

$0 ~ gname      {split ($7, T, /_/)
                 printf FMT, $1, $2, $3, $4, $5, $6, T[1], T[2] 
                }
' /tmp/disk_2016.11.29.txt 
Filesystem          	blocks	Free	% Used	%Iused	      Mounted on	      Date	    Time
/dev/sasdwk/hasasvmi	   128	   1	   127	    0%	/sasdwk/hasasvmi	2016.11.29	00.00.31
/dev/sasdd1/hasasvmi	   256	  44	   212	17.00%	/sasdd1/hasasvmi	2016.11.29	00.00.34
/dev/sasdwk/hasasvmi	   128	   1	   127	    0%	/sasdwk/hasasvmi	2016.11.29	00.30.40
.
.
.


Last edited by RudiC; 11-30-2016 at 03:40 PM..
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