The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need advice naner9 UNIX for Dummies Questions & Answers 1 11-01-2007 11:05 AM
Script Advice please? earnstaf Shell Programming and Scripting 12 06-18-2007 05:55 PM
Advice on Script greengrass Shell Programming and Scripting 4 02-11-2007 12:49 AM
first script. need help and advice. sanchopansa Shell Programming and Scripting 17 10-07-2006 08:25 AM
c-shell script advice please. killerserv Shell Programming and Scripting 2 06-20-2004 03:51 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-10-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Script Help/Advice

Alright, I feel like I have a pretty good basic knowledge of shell scripting, but this one is throwing me for a loop. I know I've seen something similar done with awk, but I couldn't find it with the search function.

I've grepped through my log file and get results like this:


Code:
--
/home/dir1/file-egress-filter-Remark : Stuff
/home/dir1/file-egress-filter-permit stuff
/home/dir1/file-egress-filter-permit other stuff
/home/dir1/file-egress-filter:permit more stuff
--
/home/dir1/other-egress-filter-Remark : Other
/home/dir1/other-egress-filter-permit other
/home/dir1/other-egress-filter-permit other others
/home/dir1/other-egress-filter:permit more other
--

This can continue on for few or many files. Notice that the last line of each has a colon ( as the last delimiter where as the others are a dash (-) and also that there is a : in the first line of each.

I want output like:


Code:
--
file-egress-filter
Remark : Stuff
permit stuff
permit other stuff
permit more stuff
--
other-egress-filter
Remark : Other
permit other
permit other others
permit more other
--

So, the stuff between each set of -- changes and I want to take the file name and use it as a header and then just print the stuff after the delimiter which is a dash for all but the last line.

Ideas?
  #2 (permalink)  
Old 10-10-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
sed sounds like an appropriate tool to split the lines up into their components.
  #3 (permalink)  
Old 10-10-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
not extremely elegant, but... you could probably improve the logic/flags:

nawk -f earnstaf.awk myGreppedLogFile
earnstaf,awk:

Code:
BEGIN {
  FS="[/]"
}
/^--/ {block++;print;next}
block {
  match($NF, "^.*-.*-[^-][^-]*-")
  printf("%s\n", substr($NF, 1, RLENGTH-1))
}
{
  !(block) && ($0 ~ ":") ? match($NF, ":[^:][^:]*$") : match($NF, "-[^-][^-]*$")
  printf("%s\n", substr($NF, RSTART+1))
  block=0
}

  #4 (permalink)  
Old 10-11-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
Quote:
Originally Posted by vgersh99 View Post
not extremely elegant, but... you could probably improve the logic/flags:

nawk -f earnstaf.awk myGreppedLogFile
earnstaf,awk:

Code:
BEGIN {
  FS="[/]"
}
/^--/ {block++;print;next}
block {
  match($NF, "^.*-.*-[^-][^-]*-")
  printf("%s\n", substr($NF, 1, RLENGTH-1))
}
{
  !(block) && ($0 ~ ":") ? match($NF, ":[^:][^:]*$") : match($NF, "-[^-][^-]*$")
  printf("%s\n", substr($NF, RSTART+1))
  block=0
}
vgersh, that looks pretty nice even though I can decipher very little of the actual awk code. It also works well with a couple of issues which are my fault for failing to include them in the example.

1.) The grep returns some "blank" lines, staying with my example they look like this:

Code:
--
/home/dir1/file-egress-filter-Remark : Stuff
/home/dir1/file-egress-filter-permit stuff
/home/dir1/file-egress-filter-
/home/dir1/file-egress-filter-permit other stuff
/home/dir1/file-egress-filter-
/home/dir1/file-egress-filter:permit more stuff
--
/home/dir1/other-egress-filter-
/home/dir1/other-egress-filter-Remark : Other
/home/dir1/other-egress-filter-permit other
/home/dir1/other-egress-filter-
/home/dir1/other-egress-filter-permit other others
/home/dir1/other-egress-filter:permit more other
--

The code is returning those as just the filename ie "other-egress-filter-" and I would like to return it as a truly blank line (under the header) Thoughts?

2.) Disregard 2 ... I was able to pipe information from the grep into the awk.
  #5 (permalink)  
Old 10-11-2007
earnstaf earnstaf is offline
Registered User
  
 

Join Date: May 2007
Posts: 113
I read up on what you were doing with match and RLENGTH and RSTART, but I'm having a little trouble wrapping my head around the code itself.

Bear with me as I try to go through this:

Code:
BEGIN {
  FS="[/]"  #setting the FS to / .. I assume for the /home/dir1/ part of my pattern space..                                         
}
/^--/ {block++;print;next}  #look for -- at the start of line.  Unsure of the significance of incrementing the block... print the -- and move to the next line 
block {
  match($NF, "^.*-.*-[^-][^-]*-")  #regexp matching the "file" portion of my pattern (file-egress-filter-) .. not sure on the exclusion of dashes ([^-])..
  printf("%s\n", substr($NF, 1, RLENGTH-1)) #print newline, then print the line, starting at the first position and the whole string minus the last character.
}
{
  !(block) && ($0 ~ ":") ? match($NF, ":[^:][^:]*$") : match($NF, "-[^-][^-]*$") #if the line was not matched on the previous regexp (block) and contains a : then match on a : then no colon until the end of the line?
  printf("%s\n", substr($NF, RSTART+1)) #kinda lost here... is it saying print 1 position after the match? ..so after the :?
  block=0  #once again, not sure on the significance of the count. 
}

So, I realize that probably way off... I'll play more with awk.
  #6 (permalink)  
Old 10-11-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926

Code:
 awk '/ :/{printf "%s-%s-%s\n%s:%s\n",$4,$5,$6,$7,$8;next}
/--/{print;next}
{FS="[-/:]";print $7}'  FS="[-/:]"


Code:
% cat file
--
/home/dir1/file-egress-filter-Remark : Stuff
/home/dir1/file-egress-filter-permit stuff
/home/dir1/file-egress-filter-permit other stuff
/home/dir1/file-egress-filter:permit more stuff
--
/home/dir1/other-egress-filter-Remark : Other
/home/dir1/other-egress-filter-permit other
/home/dir1/other-egress-filter-permit other others
/home/dir1/other-egress-filter:permit more other
--
% awk '/ :/{printf "%s-%s-%s\n%s:%s\n",$4,$5,$6,$7,$8;next}
/--/{print;next}
{FS="[-/:]";print $7}'  FS="[-/:]" file
--
file-egress-filter
Remark : Stuff
permit stuff
permit other stuff
permit more stuff
--
other-egress-filter
Remark : Other
permit other
permit other others
permit more other
--

Use nawk or /usr/xpg4/bin/awk on Solaris.

Last edited by radoulov; 10-11-2007 at 12:10 PM.. Reason: correction/addition: just saw you wanted the blanks ...
  #7 (permalink)  
Old 10-11-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
BEGIN {
  FS="[/]"
}
/^--/ {block++;print;next}
block {
  match($NF, "^.*-.*-[^-][^-]*-")
  printf("%s\n", substr($NF, 1, RLENGTH-1))
}
!match($NF, "-[^-][^-]*$") {print "";next }
{
  !(block) && ($0 ~ ":") ? match($NF, ":[^:][^:]*$") : match($NF, "-[^-][^-]*$")
  printf("%s\n", substr($NF, RSTART+1))
  block=0
}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0