![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | 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. |
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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
Quote:
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.
|
|
||||
|
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. |
|
|||||
|
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 ... |
|
|||||
|
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
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|