Normal text to table format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Normal text to table format
# 8  
Old 01-27-2013
Don't know if it's relevant, but in his sample in post #1 the requestor had input line 3 printed before input line 2, which any proposal so far ignored, and even the R. did not insist upon in his acknowledgements. Anyhow, should this be necessary, try
Code:
$ awk '{getline x; getline y; print $0"\t"y"\t"x}' file
INT1:    STR2    STR1
EXT1:    STR2    STR1
INT2:    STR2    STR1

The other proposals would be easy to convert as well, of course.
These 2 Users Gave Thanks to RudiC For This Post:
# 9  
Old 01-27-2013
Hi.
Code:
% paste - - - < data1
INT1:	STR1	STR2
EXT1:	STR1	STR2
INT2:	STR1	STR2

Also without considering the order of the tokens., as noted by RudiC.

Best wishes ... cheers, drl
# 10  
Old 01-27-2013
thanks for noticing the order, I managed to fix it by using the following code after using the code provided in the previous posts.

Code:
awk '{ t=$2 ; $2=$3; $3=t; print }'

# 11  
Old 01-27-2013
Thanks RudiC for noting the point of order we all missed.

The three awk scripts I originally posted could not as easily be fixed by the method shown in message #10 in this thread as some of the other proposed solutions. For the record, here are the three scripts I provided before updated to reverse the output order of input fields 2 and 3 no matter how many output fields there are. (The third script also corrects a bug that would be revealed if the final set of input lines increased the number of columns to be output.)
Code:
echo 'Following assumes 3 lines/row, tab separator:'
awk '{getline o3;getline o2;print $0, o2, o3}' OFS="\t" input
echo
echo 'Following assumes 3 lines/row, field width based on input:'
awk '
BEGIN { s[1] = 1; s[2] = 3; s[3] = 2} # s[input_column#] = output_column#
{       o[int((NR + 2)/3),s[++c]] = $0
        if(length($0) > w[s[c]]) w[s[c]] = length($0)
        if(c == 3) c = 0
}
END {   fmt = sprintf("%%-%ds%%-%ds%%s\n", w[1] + 2, w[2] + 2)
        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 '
# Usage: oc = oo(ic)
# NAME oo -- convert input column # to output column #
function oo(ic) {
        return(ic == 2 ? 3 : ic == 3 ? 2 : ic)
}
/:$/ {  r++
        if(c > mc) mc = c
        c = 0
}
{       o[r,oo(++c)] = $0
        if(length($0) > w[oo(c)]) w[oo(c)] = length($0)
}
END {   if(c > mo) mc - c
        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

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