Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested Awk

Status
Not open for further replies.

mcallaghan

IS-IT--Management
Mar 22, 2005
43
US
I have a list of files like so
>>C:\dev\test\test1\090205\A1CQ00004341_BTOtiers.sql@@\main\R3_Support_INT\3
>>C:\dev\test\test1\090205\A1CQ00004341_TOPtiers.sql@@\main\R3_Support_INT\3
>>C:\dev\test\test1\090205\A1CQ00004341_TOPtier2.sql@@\main\R3_Support_INT\3

What i want to to is just have a nested awk which will grab the names of the files from the lines above and send them to another document or a log file

Var1=A1CQ00004341_BTOtiers.sql
Var2=A1CQ00004341_TOPtiers.sql
var3=A1CQ00004341_TOPtier2.sql

My question is how to create a nested awk to do this.
 
If you're running under windoze:
Code:
awk -F"[\\\\@]" "{print \"Var\"NR\"=\"$6}" file1 >file2
 
considering the 'paths' to the sql file could potentially be of different length(s):
Code:
echo echo 'C:\dev\test\test1\090205\A1CQ00004341_TOPtier2.sql@@\main\R3_Support_INT\3'| nawk -F'@@' '{match($1,"[^\\\\][^\\\\]*$"); print substr($1, RSTART)}'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Yes, mine will fail if the number of fields varies.
I took the liberty of tweaking yours.

nawk -F'@@' '{match($1,/[^\\\\]+$/); print substr($1,START)}'
 
close, but.....
unfortunately Solaris nawk is NOT POSIX-complient and doesn't support ERE [first 'E' stands for 'Extended'] and therefore there's no supoprt for '+' spec.

You can use '/usr/xpg4/bin/awk' - this is the POSIX complient awk and it does support ERE [including '+'].

So the 'doubly tweaked' code looks like:
Code:
/usr/xpg4/bin/awk -F'@@' '{match($1,/[^\\\\]+$/); print substr($1,[COLOR=red]R[/color]START)}'

NOTE: the missing 'R' in 'RSTART'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I forgot that since the regexp is not in quotes we don't need so many backslashes:
[tt]/usr/xpg4/bin/awk -F'@@' '{match($1,/[^\\]+$/); print substr($1,RSTART)}'[/tt]
 
good point!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Here's a Ruby solution:
Code:
ruby -pe 'sub(/.*?([^\\]+)@@.*/,"\\1")'
Explanation:
switches: p means read & print each line
e means program follows

The parentheses in the regular expression "capture" what they enclose; the \\1 in the replacement string is the value of that capture.

Instead of simply saying [tt].*[/tt] I added [tt]?[/tt] to make it "non-greedy".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top