Format text output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format text output
# 1  
Old 07-20-2017
Format text output

Hello,

i've got a script based on queries which generates this output into a file

Code:
cat cron_S-PostGres-ncc_license_Simpel_ActSub_per_Product
 201502-b31s31i10            | MVNO          | 114751 | 2017-07-19
 201502-b31s31i10R60-60 | MVNO          |  62115   | 2017-07-19
 201511-b31s31i15            | MVNO          | 311970 | 2017-07-19
 Simpel PostPay                 | MVNO          |     59     | 2017-07-19
 Simpel PrePay                   | MVNO         |  10254   | 2017-07-19

I'm finding it a bit hard in order to try to do another script which opens this file and present the output in the following format:
Code:
201502-b31s31i10  - 114751 (2017-07-19), 201502-b31s31i10R60 - 62115 (2017-07-19), ...

I tried to play a bit with grep and awk, but i don't want to specifically grep any string from the first row since they may increase by time, making the grep static (i.e. by time the script needs to be altered to add the new string in grep.

can you kindly shed some light?
# 2  
Old 07-20-2017
Quote:
Originally Posted by nms
...
...
I tried to play a bit with grep and awk, but i don't want to specifically grep any string from the first row since they may increase by time, making the grep static (i.e. by time the script needs to be altered to add the new string in grep.
can you kindly shed some light?
It looks like you have to perform the same set of steps on each line.

Take, for example, this line:
Code:
 201502-b31s31i10            | MVNO          | 114751 | 2017-07-19

1) Split it on the "|" character, to get the following tokens:

Code:
Token 1 = "201502-b31s31i10"
Token 2 = "MVNO"
Token 3 = "114751"
Token 4 = "2017-07-19"

2) Now use tokens 1, 3 and 4 to form this string:

Code:
201502-b31s31i10 - 114751 (2017-07-19)

3) And append it to another "output" string (i.e. a string that you will print once you are done processing your entire file.)

Keep doing steps 1), 2) and 3) for all lines you go through.
In the end, print the "output" string.

Here's a Perl implementation, in case Perl is an option for you.
The -F modifier specifies the character "|" to split the line on.

Code:
$
$ cat data.txt
 201502-b31s31i10            | MVNO          | 114751 | 2017-07-19
 201502-b31s31i10R60-60 | MVNO          |  62115   | 2017-07-19
 201511-b31s31i15            | MVNO          | 311970 | 2017-07-19
 Simpel PostPay                 | MVNO          |     59     | 2017-07-19
 Simpel PrePay                   | MVNO         |  10254   | 2017-07-19
$
$
$ perl -F/\\\|/ -lane '($x,$y,$z) = map {s/^\s*//; s/\s*$//; $_ } @F[0,2,3];  # trim whitespaces from tokens 0, 2, 3
                       $s .= "$x - $y ($z), ";                                # append trimmed tokens to output string
                       END {
                           $s =~ s/, $//;                                     # trim unnecessary characters from output
                           print $s
                       }
                      ' data.txt
201502-b31s31i10 - 114751 (2017-07-19), 201502-b31s31i10R60-60 - 62115 (2017-07-19), 201511-b31s31i15 - 311970 (2017-07-19), Simpel PostPay - 59 (2017-07-19), Simpel PrePay - 10254 (2017-07-19)
$
$

# 3  
Old 07-24-2017
Hi durden_tyler,

Your perl one liner is excellent. I only just modified it since now the output needs to be slightly different from what i presented previously and it worked.

Thank you very much.

Rgds,
# 4  
Old 07-26-2017
Very helpful, thanks Tyler.
# 5  
Old 07-26-2017
Either of you two might want to click the "Thanks" button on durden_tyler's post to enforce his/her gratitude...
This User Gave Thanks to RudiC For This Post:
# 6  
Old 07-26-2017
Quote:
Originally Posted by nms
I'm finding it a bit hard in order to try to do another script which opens this file
A word of caution: UNIX text files (and the utilities working on them) are line-oriented. That means these utilities don't read from a file or input stream basically one character at a time, but they read in chunks. These chunks are series of characters separated by newline ("\n") characters ("lines").

If the line you construct gets too long (it depends on your system how long "too long" is, have a look in the kernel headers) you might eventually get a "line too long" error.

Note that it is possible to read from a file byte-based (that is: a certain number of bytes at a time, not caring about line separators), but this cannot be done in (shell) scripts using only standard methods.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop to convert text output in the HTML format

Hello Everyone, I have a sample file raw.txt as shown below : Drive Bays Bay Name : SD-2C Number of Standby Power Supplies : 4 Number of Drive Enclosures : 12 Summary Status of Contained Modules All... (6 Replies)
Discussion started by: rahul2662
6 Replies

2. Shell Programming and Scripting

Format output

Hello Team, I have the following details in a txt file (please note the spaces and tabs) C1 C2 C3 ------------------ --------------- ------------- abc, xyz 2 8 pqr 2 ... (9 Replies)
Discussion started by: H squared
9 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Read from text file;format and print output

Hi Following is the assumed input... Symmetrix ID : 12345 Originator Port wwn : 123456789 User-generated Name : 123456789/123456789 Sym Dev Dir:P LUN ------ ----- ----------------------- ---- --- ---- ---- ---- ------- 1234 ... (4 Replies)
Discussion started by: maddy.san
4 Replies

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question