|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.000from 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
|
||||
|
||||
|
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 |
| The Following User Says Thank You to Yoda For This Useful Post: | ||
Akshay Hegde (03-22-2013) | ||
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
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... |
|
#5
|
||||
|
||||
|
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 |
| The Following User Says Thank You to Yoda For This Useful Post: | ||
Akshay Hegde (03-22-2013) | ||
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
||||
|
||||
|
Code:
echo "ax:73.1(73.01)" | awk ' { gsub(/.*:|\(.*/,x) }1 'For further reference: GAWK String Functions |
| The Following User Says Thank You to Yoda For This Useful Post: | ||
Akshay Hegde (03-22-2013) | ||
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|