LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2009, 11:26 AM   #1
azkarashare
LQ Newbie
 
Registered: Mar 2008
Location: Buenos Aires, Argentina
Distribution: Slackware, Gentoo, Debian
Posts: 10

Rep: Reputation: 0
Smile How correctly parsed vars in Awk


Hi, I wrote a script for "Data Protector" that output a list and I want to filter its items and arrange them in columns. The script comes:


omnidb -session -type backup -since $Yesterday -until `date '+%Y/%m/%d'` -detail|grep -E "Specification|SessionID|Started|Finished|Host|Status"|sed 's/://g'|sed 's/Backup Specification/Specification/g'|awk '{print $1,$2$3$4$5}'

This is the list that I have from that script

SessionID 2009/08/17-42
Specification ar-gecs21_FS_Daily
Started Mon Aug 17 210002
Finished Mon Aug 17 210226
Status Completed
Host nols17
SessionID 2009/08/17-44
Specification Disco_Servers_10pm_FS_Daily-2
Started Mon Aug 17 220001
Finished Tue Aug 18 045352
Status Completed
Host nols17
SessionID 2009/08/17-47
Specification Telco-DCN-FS-Daily-22_hs
Started Mon Aug 17 220016
Finished Mon Aug 17 225652
Status Completed
Host nols17
SessionID 2009/08/17-48
Specification MSSQL TPARADM04_SQL_Daily
Started Mon Aug 17 220036
Finished Mon Aug 17 235236
Status Completed
Host nols17
SessionID 2009/08/17-51
Specification Disco_D095NWSHTEC05_FS_Daily-2
Started Mon Aug 17 221502
Finished -
Status In Progress/Errors
Host nols17



This is how I'd arranged the output in columns:

...|awk \
{
if ( $1 ~ /SessionID/ )
SI=`print $2`
printf ("%s\n",SI) ;
}'


And this is what I got when execute the script:

syntax error The source line is 4.
The error context is
>>> SI=` <<<
awk: The statement cannot be correctly parsed.
The source line is 4.


I think I'm wrong about how store "print $2" into a variable
If anybody have any comment about it I'll be very grateful
Thanks to all
 
Old 08-18-2009, 11:30 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
To store a string in a variable, just assign the value to that variable:
Code:
SI = $2
in your example you don't need to assign the variable, just print it out:
Code:
printf "%s\n", $2
which simply equals to print $2, since you use a generic format. Your script can be reduced to:
Code:
... | awk '$1 ~ "SessionID" {print $2}'

Last edited by colucix; 08-18-2009 at 11:33 AM.
 
Old 08-18-2009, 11:46 AM   #3
azkarashare
LQ Newbie
 
Registered: Mar 2008
Location: Buenos Aires, Argentina
Distribution: Slackware, Gentoo, Debian
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
To store a string in a variable, just assign the value to that variable:
Code:
SI = $2
in your example you don't need to assign the variable, just print it out:
Code:
printf "%s\n", $2
which simply equals to print $2, since you use a generic format. Your script can be reduced to:
Code:
... | awk '$1 ~ "SessionID" {print $2}'
Grate,
But now I have this output:

2009/08/17-1
2009/08/17-1
2009/08/17-1
2009/08/17-1
2009/08/17-1
2009/08/17-1
2009/08/17-4
2009/08/17-4
2009/08/17-4
2009/08/17-4
2009/08/17-4
2009/08/17-4
2009/08/17-6
2009/08/17-6
2009/08/17-6
2009/08/17-6
2009/08/17-6
2009/08/17-6
2009/08/17-8
2009/08/17-8
2009/08/17-8
2009/08/17-8
2009/08/17-8
2009/08/17-8


When I just need

2009/08/17-1
2009/08/17-4
2009/08/17-6
2009/08/17-8


I cannot use uniq at the end because the idea is:

{
if ( $1 ~ /SessionID/ )
SI=`print $2`
printf ("%s\n",SI) ;
}'
{
if ( $1 ~ /Specification/ )
SP=`print $2`
printf ("%s\n",SP) ;
}'
{
if ( $1 ~ /Status/ )
ST=`print $2`
printf ("%s\t%s\t%s\n",SI,SP,ST) ;
}'
.....


¿?
 
Old 08-18-2009, 11:51 AM   #4
azkarashare
LQ Newbie
 
Registered: Mar 2008
Location: Buenos Aires, Argentina
Distribution: Slackware, Gentoo, Debian
Posts: 10

Original Poster
Rep: Reputation: 0
sorry
I meant when I replied:

{
if ( $1 ~ /SessionID/ )
SI= $2

if ( $1 ~ /Specification/ )
SP= $2

if ( $1 ~ /Status/ )
ST= $2
printf ("%s\t%s\t%s\n",SI,SP,ST) ;
}'
.....
 
Old 08-18-2009, 12:01 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I don't understand what you really want to obtain. Please, write a sample of the desired output (full version, not only the first part of the lines).
 
Old 08-18-2009, 12:08 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
In any case take in mind that the repetition is because the printf statement is executed for each input line. If you want to execute it inside the last if block you have to use brackets, since you use a multi-line block of code:
Code:
{
  if ( $1 ~ /SessionID/ )
     SI = $2

  if ( $1 ~ /Specification/ )
     SP = $2

  if ( $1 ~ /Status/ ) {
     ST = $2
     printf ("%s\t%s\t%s\n",SI,SP,ST)
  }
}
 
Old 08-18-2009, 01:52 PM   #7
azkarashare
LQ Newbie
 
Registered: Mar 2008
Location: Buenos Aires, Argentina
Distribution: Slackware, Gentoo, Debian
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks a lot Colucix!!!
You realy have it clear.
Here is how the script left:

...
{
if ( $1 ~ /SessionID/ )
SI= $2
if ( $1 ~ /Started/ )
ST= $2
if ( $1 ~ /Finished/ )
FN= $2
if ( $1 ~ /Host/ )
HT= $2
if ( $1 ~ /Status/ )
TT= $2
if ( $1 ~ /Specification/ ) {
SP= $2
printf ("%s\t%s\t%s\t%s\t%s\t%s\n",SI,ST,FN,HT,TT,SP)
}
}'



And the output:

[nols17 /home/root/Azk]# ./version2
Session_ID Started Finished Host Status Specification
____________________________________________________________________________________________________ ___________________
2009/08/17-1 MonAug17000001 MonAug17154414 nols17 Completed Telco-DCN_FS_Daily
2009/08/17-4 MonAug17000001 MonAug17154423 nols17 Completed IT_Archivelog_FS_Daily
2009/08/17-6 MonAug17001501 MonAug17001858 nols17 Completed MSSQLPaxMobiles_SQL-Dbserver1_Daily
2009/08/17-8 MonAug17013001 MonAug17020827 nols17 Completed Disco_Servers_230am_FS_Daily
2009/08/17-10 MonAug17023001 MonAug17023314 nols17 Completed GE_OL_FS_Daily
2009/08/17-12 MonAug17030001 MonAug17075232 nols17 Completed Disco_D208srtec07_FS_Daily
2009/08/17-14 MonAug17040001 MonAug17090719 nols17 Completed/Failures Disco_Servers_4am_FS_Daily
2009/08/17-17 MonAug17040007 MonAug17053526 nols17 Completed IT_UnixFS_Daily2
2009/08/17-20 MonAug17061501 MonAug17062904 nols17 Completed IT_UnixFS_Daily3
2009/08/17-22 MonAug17061515 MonAug17062500 nols17 Completed Disco_Servers_7am_FS_Daily
2009/08/17-24 MonAug17070001 MonAug17070655 nols17 Completed Servers_standard_FS_Daily
2009/08/17-26 MonAug17070016 MonAug17103530 nols17 Completed/Errors Disco_Servers_10pm_FS_Weekly-2
2009/08/17-28 MonAug17073115 MonAug17122505 ar-nolp21.ol.mr Failed Disco_D095NWSHTEC05_FS_Daily
2009/08/17-30 MonAug17080001 MonAug17100446 nols17 Completed ar-gecs22_FS_Daily
2009/08/17-32 MonAug17131502 MonAug17144902 nols17 Completed ar-gecs43-ar-gecs42_FS_Daily
2009/08/17-34 MonAug17131504 MonAug17132204 nols17 Completed Ar-nols_Daily
2009/08/17-40 MonAug17131507 MonAug17133849 nols17 Completed Omniback_Database_Daily
2009/08/17-42 MonAug17164502 MonAug17213649 nols17 Completed ar-gecs21_FS_Daily
2009/08/17-44 MonAug17210002 MonAug17210226 nols17 Completed Disco_Servers_10pm_FS_Daily-2
2009/08/17-47 MonAug17220001 TueAug18045352 nols17 Completed Telco-DCN-FS-Daily-22_hs
2009/08/17-48 MonAug17220016 MonAug17225652 nols17 Completed MSSQLTPARADM04_SQL_Daily
2009/08/17-51 MonAug17220036 MonAug17235236 nols17 Completed Disco_D095NWSHTEC05_FS_Daily-2



Thanks a lot again!!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php not being parsed properly hiren_bhatt Linux - General 3 04-01-2007 02:20 AM
/etc/modprobe.conf not being parsed? mrjamin Fedora 1 01-18-2006 02:33 PM
When is hosts.deny parsed? juanbobo Linux - General 1 11-29-2005 09:32 PM
passing vars to awk schneidz Programming 6 09-25-2005 01:52 PM
Non-Parsed-Header scripts soulsniper Linux - Software 0 11-22-2003 02:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration