Sponsored Content
Full Discussion: Format text output
Top Forums Shell Programming and Scripting Format text output Post 303000836 by durden_tyler on Thursday 20th of July 2017 02:18:24 PM
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)
$
$

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

10. 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
MDOM::Token::Comment(3pm)				User Contributed Perl Documentation				 MDOM::Token::Comment(3pm)

NAME
MDOM::Token::Comment - A comment in Makefile source code INHERITANCE
MDOM::Token::Comment isa MDOM::Token isa MDOM::Element SYNOPSIS
# This is a MDOM::Token::Comment foo: bar # So is this one echo 'hello' DESCRIPTION
In MDOM, comments are represented by "MDOM::Token::Comment" objects. These come in two flavours, line comment and inline comments. A "line comment" is a comment that stands on its own line. These comments hold their own newline and whitespace (both leading and trailing) as part of the one "MDOM::Token::Comment" object. An inline comment is a comment that appears after some code, and continues to the end of the line. This does not include whitespace, and the terminating newlines is considered a separate MDOM::Token::Whitespace token. This is largely a convenience, simplifying a lot of normal code relating to the common things people do with comments. Most commonly, it means when you "prune" or "delete" a comment, a line comment disappears taking the entire line with it, and an inline comment is removed from the inside of the line, allowing the newline to drop back onto the end of the code, as you would expect. It also means you can move comments around in blocks much more easily. For now, this is a suitably handy way to do things. However, I do reserve the right to change my mind on this one if it gets dangerously anachronistic somewhere down the line. METHODS
Only very limited methods are available, beyond those provided by our parent MDOM::Token and MDOM::Element classes. line The "line" accessor returns true if the "MDOM::Token::Comment" is a line comment, or false if it is an inline comment. SUPPORT
See the support section in the main module. AUTHOR
Adam Kennedy <adamk@cpan.org> COPYRIGHT
Copyright 2001 - 2006 Adam Kennedy. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. perl v5.12.4 2011-08-28 MDOM::Token::Comment(3pm)
All times are GMT -4. The time now is 09:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy