-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
print "Example 1"
skip to "more"
copy all
This script will print the line "Example 1" to standard out, then read the input file until a line is encountered which contains the substring "more". That line and any that follow it are copied to output. When not inside a foreach statement, the statements skip, copy, and replace may be applied to a sequence of statements determined by a control expression which is typically a regular expression matched against the current input line.
print "#!/bin/bash -x"
foreach all
if "^(.*)/TARGETS-" then
dir = $1
if "name\s*=\s*'(.*)'" then
print "./runbench.sh $dir:$1 _bin/$dir/$1"
end
end
end
This rsed script translates an input file into building a bash script. It begins by printing the common Linux script marker on the first line. Then, it examples every input line looking for the regular expression in the predicate position of the first if. The matches for this expression are available in the "dynamic variables" $0 (the entire match), $1 (the match for the first parenthesized term), and so on with larger numbers. Here we assign the prefix of the match to the variable dir. The left hand side of an assignment may be just the identifier or it may be prefixed with the variable marker as in $dir. Both forms are equivalent. A second if matches against the same input line. These two conditions could be expressed in a single regular expression but sometimes it is easier understand when decomposed. The print statement gives an illustrate of variable references and implicit variable substitution within strings. The string output will have the variable references $dir and $1 replaced with the values. Note that the dynamic variables such as $1 always refer to the most recent match and so sometimes need to be saved in variables like dir to be used later.
This example translates a CSV file into a MySQL insert query.
print "insert into $PERF_TABLE ( $CURRENT ) VALUES "
skip for 1
sep = ""
foreach all
print '$sep($CURRENT)'
sep = ','
end
print ";"
This example is similar to previous with the wrinkle that we need a comma separator between value lines. Variable $PERF_TABLE is not defined in the script and rsed will use the value in the environment variable with the same name and will use the empty string failing that. Variable "CURRENT" refers to the current value of the input line.