awk - advance when condition met?
by babag from LinuxQuestions.org on (#5F02J)
i have code that uses awk and searches a text file for matches. the text file is a list of scripts and macros that the scripts are used in. the code gives a printout listing each script followed by the macros it appears in. this is the format:
Code:script
macro
macro
macro
----------------------the problem i have is that, when a script is used more than once in a macro, the script's id appears for each use so the macro is printed for each use, an unnecessary redundancy. i need to find a way to jump ahead to the next line as soon as the first match is found. i just need to know the script is used, not how many times.
i'm a total non-coder so this is hard for me but i do grasp some of it. the structure of the text file is as follows:
each line of the text file is either a script or macro and is indicated in $1 as either "SCR" or "ACT".
$1 indicates either "SCR" or "ACT"
$4 is a long text string that is a unique id for the SCR or ACT
$6 is the name of the script or macro
$7 - NF list the id for each script, prepending a "_", in order of its use in the macro (if a script is used more than once, the id appears again for each use)
Code:awk '
NR == FNR {
if ($1 == "SCR") script["_"$4] = $6
next
}
$1 == "ACT" {
for (i=7; i<=NF; i++)
if ($i in script)
script_uses[$i] = script_uses[$i] $6 ORS
}
END {
for (id in script_uses)
printf "%s is used in macros:\n%s\n----------------------\n", script[id], script_uses[id]
}
' reaper-kb_COPY.ini reaper-kb_COPY.inias i understand it, the code runs through the text file twice, once finding the scripts (using the $1 "SCR") and their $4 id's, and a second time matching those id's in $7 - NF.
how to alter this to jump ahead as soon as an id match is made?
thanks and i hope this is clear,
babag


Code:script
macro
macro
macro
----------------------the problem i have is that, when a script is used more than once in a macro, the script's id appears for each use so the macro is printed for each use, an unnecessary redundancy. i need to find a way to jump ahead to the next line as soon as the first match is found. i just need to know the script is used, not how many times.
i'm a total non-coder so this is hard for me but i do grasp some of it. the structure of the text file is as follows:
each line of the text file is either a script or macro and is indicated in $1 as either "SCR" or "ACT".
$1 indicates either "SCR" or "ACT"
$4 is a long text string that is a unique id for the SCR or ACT
$6 is the name of the script or macro
$7 - NF list the id for each script, prepending a "_", in order of its use in the macro (if a script is used more than once, the id appears again for each use)
Code:awk '
NR == FNR {
if ($1 == "SCR") script["_"$4] = $6
next
}
$1 == "ACT" {
for (i=7; i<=NF; i++)
if ($i in script)
script_uses[$i] = script_uses[$i] $6 ORS
}
END {
for (id in script_uses)
printf "%s is used in macros:\n%s\n----------------------\n", script[id], script_uses[id]
}
' reaper-kb_COPY.ini reaper-kb_COPY.inias i understand it, the code runs through the text file twice, once finding the scripts (using the $1 "SCR") and their $4 id's, and a second time matching those id's in $7 - NF.
how to alter this to jump ahead as soon as an id match is made?
thanks and i hope this is clear,
babag