Explanation regarding escape formating | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Explanation regarding escape formating

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-22-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 20 April 2014, 9:36 AM EDT
Location: Goa, India
Posts: 1,190
Thanks: 54
Thanked 339 Times in 313 Posts
Explanation regarding esp formating

Hi ! experts

I would like to understand escape formating in awk, please anyone explain about the same using following datafile


Code:
 DATA SET: ./myfile.asc
             TIME: 29-OCT-2011 08:15 to 15-DEC-2011 16:15
             ax: 73.1
             aY: 15.2
             aZ: -8.361
 Column  1: var_1 is xyz (cm/s)
 Column  2: var_2 is axyz (cm/s)
 Column  3: var_3 is abcde (cm/s)
 Column  4: cnst is constant
                            var_1  var_2 var_3  cnst
29-OCT-2011 08:30:00 /    1:   ....   ....   ....  1.000
29-OCT-2011 09:00:00 /    2:   ....   ....   ....  1.000
29-OCT-2011 09:30:00 /    3:   ....   ....   ....  1.000
29-OCT-2011 10:00:00 /    4:   ....   ....   ....  1.000
29-OCT-2011 10:30:00 /    5:   ....   ....   ....  1.000

from datafile I want to print like below

Code:
29-OCT-2011 08:15  15-DEC-2011 16:15 73.1 15.2 -8.361 29-OCT-2011 08:30:00       ....   ....   ....  1.000
29-OCT-2011 08:15  15-DEC-2011 16:15 73.1 15.2 -8.361 29-OCT-2011 09:00:00       ....   ....   ....  1.000
29-OCT-2011 08:15  15-DEC-2011 16:15 73.1 15.2 -8.361 29-OCT-2011 09:30:00       ....   ....   ....  1.000
29-OCT-2011 08:15  15-DEC-2011 16:15 73.1 15.2 -8.361 29-OCT-2011 10:00:00       ....   ....   ....  1.000
29-OCT-2011 08:15  15-DEC-2011 16:15 73.1 15.2 -8.361 29-OCT-2011 10:30:00       ....   ....   ....  1.000

also please explain the same so that I can learn something from you

Last edited by Akshay Hegde; 03-22-2013 at 11:52 AM.. Reason: to edit wrong title
Sponsored Links
    #2  
Old 03-22-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 April 2014, 8:35 PM EDT
Location: Galactic Empire
Posts: 3,282
Thanks: 227
Thanked 1,157 Times in 1,094 Posts
I didn't quite understand what you meant by escape formatting.

But you can produce your expected output using:

Code:
awk '
        /TIME:/ {
                        T = $0
                        gsub(/[ \t]*TIME: | to/, x, T)
        }
        /ax:/ {
                        AX = $NF
        }
        /aY:/ {
                        AY = $NF
        }
        /aZ:/ {
                        AZ = $NF
        }
        /^[0-9]/ {
                        R = $0
                        sub(/\/[ \t]*[0-9]*:/, x, R)
                        print T, AX, AY, AZ, R
        }
' datafile

Sponsored Links
    #3  
Old 03-22-2013
wisecracker's Avatar
wisecracker wisecracker is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 20 April 2014, 4:23 AM EDT
Location: Loughborough
Posts: 494
Thanks: 101
Thanked 97 Times in 84 Posts
Google is your friend here:-


Code:
http://ascii-table.com/ansi-escape-sequences.php

Some of these can be used in awk...

Basic ones are...

Code:
1) Colours:  ......"\033[0;31;40m Some text..."...... Gives '\033' an octal value for character 27, '[' separater for the colours needed, '0' select standard _font_, ';' further separaters, '31' foreground colour, '40' background colour, 'm' notifies that colours and _fonts_ are going to be generated. FG and BG colours have a selection of 8 basic colours

2) Force a print porition: "\033[10;20f Some text..."...... Will position the cursor to 10 lines down and 20 characters across, 'f' here meaning FORCE...

Many others available just Google...

NOTE: Some ANSI Esc codes don't work inside some shells/terminals...
    #4  
Old 03-22-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 20 April 2014, 9:36 AM EDT
Location: Goa, India
Posts: 1,190
Thanks: 54
Thanked 339 Times in 313 Posts
Please mr Yoda explain code..and thanks for informing about wrong title
Sponsored Links
    #5  
Old 03-22-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 April 2014, 8:35 PM EDT
Location: Galactic Empire
Posts: 3,282
Thanks: 227
Thanked 1,157 Times in 1,094 Posts
The code is pretty much straightforward. Here is the explanation:

Code:
awk '
        /TIME:/ {                                       # Search pattern: TIME:
                        T = $0                          # If found assign current record: $0 to variable: T
                        gsub(/[ \t]*TIME: | to/, x, T)  # Substitute 0 or more occurrence of space or tab followed by TIME: with null in variable: T
        }                                               # Also Substitute space followed by to with null in variable: T

        /ax:/ {                                         # Search pattern: ax:
                        AX = $NF                        # If found assign last field: $NF to variable: AX
        }

        /aY:/ {                                         # Search pattern: aY:
                        AY = $NF                        # If found assign last field: $NF to variable: AY
        }

        /aZ:/ {                                         # Search pattern: aZ:
                        AZ = $NF                        # If found assign last field: $NF to variable: AZ
        }

        /^[0-9]/ {                                      # Search pattern: ^[0-9] (record starting with digits)
                        R = $0                          # If found assign current record: $0 to variable: R
                        sub(/\/[ \t]*[0-9]*:/, x, R)    # Substitute 0 or more occurrence of space or tab & by 0 or more occurrence of digits with null
                        print T, AX, AY, AZ, R          # Print values of all variables.
        }

' file

Sponsored Links
    #6  
Old 03-22-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 20 April 2014, 9:36 AM EDT
Location: Goa, India
Posts: 1,190
Thanks: 54
Thanked 339 Times in 313 Posts
Thank you so much Yoda

if suppose pattern say in current example
Code:
ax:73.1(73.01)

then how to use gsub I need only first
Code:
73.1

Sponsored Links
    #7  
Old 03-22-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 April 2014, 8:35 PM EDT
Location: Galactic Empire
Posts: 3,282
Thanks: 227
Thanked 1,157 Times in 1,094 Posts

Code:
echo "ax:73.1(73.01)" | awk ' { gsub(/.*:|\(.*/,x) }1 '

For further reference: GAWK String Functions
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Text formating jaydeep_sadaria Shell Programming and Scripting 3 11-15-2009 07:48 PM
formating output richsark Shell Programming and Scripting 2 05-06-2009 01:49 PM
Output formating jaydeep_sadaria Shell Programming and Scripting 1 04-10-2008 12:39 PM
formating date ragha81 Shell Programming and Scripting 2 01-05-2007 10:20 AM
Formating in Echo? redlotus72 UNIX for Dummies Questions & Answers 4 03-31-2005 06:04 AM



All times are GMT -4. The time now is 12:08 PM.