Normal text to table format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Normal text to table format
# 1  
Old 01-26-2013
Normal text list to table format

Hi,

I am trying to show my list, from a simple list format to a table (row and column formatted table)

Currently i have this format in my output (the formart it will always be like this ) >> first 3 lines must be on the same line aligned, and the next 3 shud be on 2nd line....:

Code:
INT1:
STR1
STR2
EXT1:
STR1
STR2
INT2:
STR1
STR2

And the output format i'm trying to acquire is as follow

Code:
INT1:     STR2     STR1
EXT1:     STR2     STR1
INT2:     STR2     STR1

I was wondering if you could give me some hints on how to achieve this.

Thank You

Last edited by Scrutinizer; 01-27-2013 at 06:57 AM.. Reason: quote tags => code tags
# 2  
Old 01-26-2013
Code:
awk 'BEGIN{c=0}{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' filename

If you want the results to be tab separated, then try:
Code:
awk 'BEGIN{c=0;FS="\t"}{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' file

Note: Use nawk instead for Solaris or SunOS
This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-27-2013
thx for the quick reply.

Code:
awk 'BEGIN{c=0;FS="\t"}{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' file

This line of code is doing what I asked for. But I have one question, There is only a one space difference btw the different data in one line and it's not well aligned.
Code:
aaa bbb ccc
aaaaa bbb ccc

To fix this I tried to modify the code like this
Code:
awk 'BEGIN{c=0;FS="     "}{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' file

and
Code:
awk 'BEGIN{c=0;FS="\t\t"}{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' file

But no luck, If the solution to my problem is easy can u please indicate to me which one i shud change.

thanks
# 4  
Old 01-27-2013
Set OFS as well and try:
Code:
awk 'BEGIN{c=0;FS="\t";OFS="\t"}{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 01-27-2013
Here are three different awk scripts to do this making different assumptions about the number of columns/row, and width of columns. If you input data is always less that 8 output columns per input line and you always have three input lines per output row, the 1st script is very simple.

If your input data has input lines that vary in length, but there are always three input lines per output row, the 2nd awk script below adjusts the output to match input field widths.

If you have a variable number of input lines per output row but the column 1 input line data always ends with a colon, the 3rd awk script below will adjust the number of rows and column widths based on the input file contents.

Here are the three awk scripts:
Code:
echo 'Following assumes 3 lines/row, tab separator:'
awk '{printf("%s%s", $0, NR % 3 ? "\t" : "\n")}' input
echo
echo 'Following assumes 3 lines/row, field width based on input:'
awk '
{       o[int((NR + 2)/3),++c] = $0
        if(length($0) > w[c]) w[c] = length($0)
        if(c == 3) c = 0
}
END {   fmt = sprintf("%%-%ds%%-%ds%%s\n", w[1] + 2, w[2] + 2)
#printf("fmt=%s\n", fmt)
        for(i = 1; i <= NR / 3; i++)
                printf(fmt, o[i,1], o[i,2], o[i,3])
}' input
echo
echo 'Following assumes Column 1 data ends with ":", field width based on input:'
awk '
/:$/ {  r++
        if(c > mc) mc = c
        c = 0
}       
{       o[r,++c] = $0
        if(length($0) > w[c]) w[c] = length($0)
}       
END {   for(i = 1; i <= r; i++) {
                for(j = 1; j < mc; j++)
                        printf("%-*s", w[j] + 2, o[i, j])
                printf("%s\n", o[i, mc])
        }       
}' input

When these three scripts are given the a file named input containing:
Code:
INT1:
STR1
STR2
EXT1:
STR1
STR2
INT2:
STR1
STR2
Longer Column 1:
Column2
column three
column four
column 5
c6
2nd C1:
2nd C2
2nd C3
2nd C4
2nd C5
Second Column 6

the output produced is:
Code:
Following assumes 3 lines/row, tab separator:
INT1:   STR1    STR2
EXT1:   STR1    STR2
INT2:   STR1    STR2
Longer Column 1:        Column2 column three
column four     column 5        c6
2nd C1: 2nd C2  2nd C3
2nd C4  2nd C5  Second Column 6

Following assumes 3 lines/row, field width based on input:
INT1:             STR1      STR2
EXT1:             STR1      STR2
INT2:             STR1      STR2
Longer Column 1:  Column2   column three
column four       column 5  c6
2nd C1:           2nd C2    2nd C3
2nd C4            2nd C5    Second Column 6

Following assumes Column 1 data ends with ":", field width based on input:
INT1:             STR1     STR2
EXT1:             STR1     STR2
INT2:             STR1     STR2
Longer Column 1:  Column2  column three  column four  column 5  c6
2nd C1:           2nd C2   2nd C3        2nd C4       2nd C5    Second Column 6

As always, if you're using a Solaris/Sun OS system, use /usr/xpg4/bin/awk or nawk instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 01-27-2013
Thank you very much for the detailed explanation, I got more than I asked for.
Everything is working as expected Smilie
# 7  
Old 01-27-2013
@bipinajith
You set the c=0 in all your example.
This is not needed, since a non declared variable is 0, so this can be removed on all your example.
Code:
awk 'BEGIN{c=0}{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' filename

is the same as
Code:
awk '{c++}c<3{ORS=FS}c==3{ORS=RS;c=0}1' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell output format like table

Hi, OS: Redhat 7.5 shell: Bash Wrote below script to login into oracle via shell script and trying to reset locked account..It works as expected. But I need specific output << EOF should go to target terminal not all out put running below script from ansible command line.. #!/bin/bash... (1 Reply)
Discussion started by: onenessboy
1 Replies

2. Shell Programming and Scripting

Output in table format

I have one script which generate file called report.txt having following output parameter_name status comment banking ok NA finance 30% hike NA Loan_department ok 20% HR_Group defaulters Ajay I wanted to convert this file into tabular form. You can see each line contain 3 words and... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

3. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

4. Shell Programming and Scripting

convert the output in table format

Hi All, I have a output like below values val1=test.com val2=10.26.208.11 val3=en1 val4=test-priv1.com val5=192.168.3.4 val6=en2 val7=test-priv2.com val8=192.168.4.4 val9=en3 val10=test-vip.com val11=10.26.208.9 val12=$val3 I want to convet this output values into below... (1 Reply)
Discussion started by: kamauv234
1 Replies

5. Shell Programming and Scripting

Table format

My Code Hi Friends, I need to get output in table format using unix shell script.For example my server CPU and memory utilization report will come as a mail with ordinary format but i want to make as table format. Here two output finally we are getting:- CPU utilization is... (2 Replies)
Discussion started by: susindram
2 Replies

6. Shell Programming and Scripting

EBCDIC Format to Normal String

Hi, I have EBCDIC format file and i wold like to convert normal string(user readable) in unix . The source having the binary IBM file format Sample Source Format: ... (3 Replies)
Discussion started by: koti_rama
3 Replies

7. UNIX for Dummies Questions & Answers

Normal format and scientific combined data

hi guys, i have a data with a column of p value (normal format and scientific combined). i want to creat a subset of data which only contains p-value: data 1: p<10^7 data 2: p<0.01 how should i do it? many thanks! data looks like: rs7841347 128887490 1.695e-007 rs1241347 ... (4 Replies)
Discussion started by: forevertl
4 Replies

8. Programming

Create table with date format.

Hello, Could you please let me know the correct format of CREATE TABLE statement with DATE format. CREATE TABLE EMP_TABLE1 ( NAME VARCHAR(6) PRIMARY KEY, ADDRESS VARCHAR(6), BIRTH_DATE DATE ); I want BIRTH_DATE to be in "YYYYMMDDHHMISS" format. How we can create table with... (4 Replies)
Discussion started by: Poonamol
4 Replies

9. Shell Programming and Scripting

mailx requirement - email body header in bold and data content in normal text

Dear all- I have a requirement to send an email via email with body content which looks something below- Email body contents -------------------- RequestType: Update DateAcctOpened: 1/5/2010 Note that header information and data content should be normal text.. Please advice on... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

10. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies
Login or Register to Ask a Question