Format output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format output
# 1  
Old 09-30-2015
Format output

Hello Team,

I have the following details in a txt file (please note the spaces and tabs)

Code:
C1                           C2                 C3
------------------ --------------- -------------
abc, xyz                    2                   8
pqr                           2                   3
egf, uvw a                 4                   2

I wish to convert this file into a .xls file with C1 (all the text under it, included) as Column A, C2 as Column B and C3 as Column C, in the spreadsheet

Example : In Column A, we must have abc,xyx ; pqr ; egf,uvw a in Row 1,2,3 respectively alongwith their respective values in for C2 and C3 in columns B and C respectively

Could you please suggest / help me in this.

Thanks,
Haider

Last edited by Don Cragun; 09-30-2015 at 02:55 PM.. Reason: Add CODE tags for the 2nd time in this post~
# 2  
Old 09-30-2015
Please use code tags as required by forum rules!

I can't see <TAB>s, just spaces; should there be <TAB> field delimiters, why don't you replace those by semicolons and read that file in as a .csv file?
# 3  
Old 09-30-2015
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

I can't see <TAB>s, just spaces; should there be <TAB> field delimiters, why don't you replace those by semicolons and read that file in as a .csv file?

I tried using code. However, i got a message mentioning about restricted visibility

Adding the code for better understanding

---------- Post updated at 12:57 PM ---------- Previous update was at 12:56 PM ----------

Could you please elaborate your thought with an example.
# 4  
Old 09-30-2015
Assuming those wide spaces in post#1 to be <TAB>s, try e.g.
Code:
tr '\t' ';' < file
C1;C2;C3
------------------;---------------;-------------
abc, xyz;2;8
pqr;2;3
egf, uvw a;4;2

and read in the result as a .csv file.
# 5  
Old 09-30-2015
Hello

Quote:
I tried using code. However, i got a message mentioning about restricted visibility
What do you mean by that?

Regarding your added code:
Code:
Last edited by Don Cragun; 22 Minutes Ago at 19:55.. Reason: Add CODE tags for the 2nd time in this post~

It still has no tabs, just space chars.

And sharing thoughts, what have you tried so far?
# 6  
Old 09-30-2015
Quote:
Originally Posted by H squared
Hello Team,

I have the following details in a txt file (please note the spaces and tabs)

Code:
C1                           C2                 C3
------------------ --------------- -------------
abc, xyz                    2                   8
pqr                           2                   3
egf, uvw a                 4                   2

I wish to convert this file into a .xls file with C1 (all the text under it, included) as Column A, C2 as Column B and C3 as Column C, in the spreadsheet

Example : In Column A, we must have abc,xyx ; pqr ; egf,uvw a in Row 1,2,3 respectively alongwith their respective values in for C2 and C3 in columns B and C respectively

Could you please suggest / help me in this.

Thanks,
Haider
You ask us to note spaces and tabs, but you didn't use CODE tags (which are required for us to be able to distinguish tabs and multiple spaces from single spaces). You got an email message and an infraction explaining how to use CODE tags, and Corona688 edited your message to add CODE tags for you. You then edited your post again throwing away the CODE tags Corona688 was nice enough to add for you. I have added them for you again. Please review the following before editing any more of your existing posts or creating any new posts:

Moderator's Comments:
Mod Comment To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.


Then note that there are no tab characters at all in the first post in this thread and note that the spacing in the 1st column of the input you say you have does not match the spacing in your description of the output that follows (is removing the space after the comma accidental or intentional).

You haven't told us what operating system or shell you're using, and some of the things you're trying to do might be easier on some operating systems. Some versions of perl with some add-on packages can produce Microsoft proprietary Excel files, but, as RudiC suggested, you can produce a character separated values file using a semicolon as the field separator and Excel will be able to load your spreadsheet from that file. For example, you could try something like:
Code:
awk -F'[[:space:]][[:space:]]+' 'NR > 2 {print $1, $2, $3}' OFS=";" input.txt > output.csv

or, if you want to get rid of a space after a comma in the 1st field:
Code:
awk -F'[[:space:]][[:space:]]+' 'NR > 2 {gsub(", ", ",", $1); print $1, $2, $3}' OFS=";" input.txt > output.csv

If you want to try either of the above on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 10-01-2015
Thank you.

the code :
Code:
awk -F'[[:space:]][[:space:]]+' 'NR > 2 {print $1, $2, $3}' OFS=";" input.txt

has worked for me

Now I wish to have the ";" de-limited output converted into a .xls file with the following columns :


Code:
C1      C2      C3

where data from the output ";" de-limited file would be filled in the respective columns.

I wish to send this excel as an attachment in an e-mail.

OS details : SUSE Linux Enterprise Server 11 SP1 (x86_64)
Shell details : /bin/bash

Last edited by Don Cragun; 10-05-2015 at 05:56 AM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format with output

I have written some scripts that resulted in the table below (Column1 is ITEM, Column2 is Group, Column3 is Category and Column4 is Quantity) but I want the output in another format: Input: K123 X CATA 3 K123 Y CATA 4 K123 Z CATA 2 K123 X CATB 5 K123 Y CATB 2 K123 Z CATB 2 B65 M CATB... (7 Replies)
Discussion started by: aydj
7 Replies

2. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

3. UNIX for Dummies Questions & Answers

Format Output

Hello Learned People, Good evening. I have absolutely no idea as how to do this & I admit that I do not know anything in unix. I must learn this one of these days. Im a help desk incharge today & someone gave me this file to be formatted & I have a file like this. vi NewFile.txt 232... (8 Replies)
Discussion started by: RTAN
8 Replies

4. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

5. UNIX for Advanced & Expert Users

format df -k output

i am running df -k command on aix machine. i got the output like this. i need to store into file and send that file into microsoft excel.i need to allign properly. Filesystem 512 blocks Free % Used Iused %Iused Mounted on /dev/hd4 262144 126488 52% ... (9 Replies)
Discussion started by: wintercoat
9 Replies

6. Shell Programming and Scripting

Help in Getting specified output format

Hi all, I have input text file of this format objectclass:endeavor pid:12345 postalAddress:379 PROSPECT ST street:STE B l:TORRINGTON st:CT postalCode:067905238 telephoneNumber:9999999999... (2 Replies)
Discussion started by: pintoo
2 Replies

7. Shell Programming and Scripting

How to format output

Dear all, I have written a program which access database and displays the values returned by the query . There are 10 columns to be displayed in one row. But am ending with 5 lines displayed on 1st line and the next 5 in the 2nd line. Ex : 21608 10-20-2007 148 Al's Appliance... (7 Replies)
Discussion started by: uday542
7 Replies

8. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

9. Shell Programming and Scripting

format output

I have a text file that I want a script to parse and grab only the relavent stuff. No idea where to start.. This is the text file.212 0.00000 ? -> (multicast) ETHER Type=2000 (Unknown), size = 344 bytes 0: 0100 0ccc cccc 000b 5f95 0fbe 014a aaaa ........_....J..... (7 Replies)
Discussion started by: Tornado
7 Replies

10. UNIX for Dummies Questions & Answers

ls output format

below is the output is ls -l -rw-r--r-- 1 tonyt staff 3212 Apr 17 1999 file1 -rw-r--r-- 1 tonyt staff 4541 Mar 3 21:08 file2 why the date format is not the same? (6 Replies)
Discussion started by: tonyt
6 Replies
Login or Register to Ask a Question