Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I have a shell script with (1000) contiguous textlines like ampl Models/aircrftb.mod > Output/aircrftb.out ampl Models/airport.mod > Output/airport.out ampl Models/aljazzaf.mod > Output/aljazzaf.out ampl Models/allinitc.mod > Output/allinitc.out ampl Models/allinit.mod > Output/allinit.out ampl Models/allinitu.mod > Output/allinitu.out ampl Models/alsotame.mod > Output/alsotame.out before each line I want to insert a new line to print the model name into a TeX file, like printf '%s' " aircrftb & " >> TABLE.TEX and printf '%s' " airport & " >> TABLE.TEX etc. How can I do this in sed or awk? Any help is greatly appreciated. Dieter kraft@hm.edu
Post Follow-up to this messageDieter Kraft wrote:
> Hi,
>
> I have a shell script
> with (1000) contiguous textlines like
>
> ampl Models/aircrftb.mod > Output/aircrftb.out
> ampl Models/airport.mod > Output/airport.out
> ampl Models/aljazzaf.mod > Output/aljazzaf.out
> ampl Models/allinitc.mod > Output/allinitc.out
> ampl Models/allinit.mod > Output/allinit.out
> ampl Models/allinitu.mod > Output/allinitu.out
> ampl Models/alsotame.mod > Output/alsotame.out
>
> before each line I want to insert a new line to print
> the model name into a TeX file, like
>
> printf '%s' " aircrftb & " >> TABLE.TEX
> and
> printf '%s' " airport & " >> TABLE.TEX
> etc.
>
> How can I do this in sed or awk?
(assuming the .mod and the .out name are always the same)
awk -v sq="'" -v dq='"' -F '[/.]' '{
print "printf "sq"%s"sq" "dq" "$(NF-1)" & "dq" >> TABLE.TEX";
print}' yourscript.sh
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Post Follow-up to this messagepk wrote:
> Dieter Kraft wrote:
>
>
> (assuming the .mod and the .out name are always the same)
>
> awk -v sq="'" -v dq='"' -F '[/.]' '{
> print "printf "sq"%s"sq" "dq" "$(NF-1)" & "dq" >> TABLE.TEX";
> print}' yourscript.sh
>
Thanks a lot; it's exactly what I need, but it is cryptic for me.
If you have some time could you explain roughly what you did.
Dieter
--
Dieter Kraft
Munich University of Applied Sciences
Department of Mechanical Engineering
dieter.kraft@hm.edu
www.lrz-muenchen.de/~dkraft
Post Follow-up to this messageDieter Kraft wrote:
> Thanks a lot; it's exactly what I need, but it is cryptic for me.
> If you have some time could you explain roughly what you did.
> Dieter
Well, let's put some comments in.
# input lines are like
#
# ampl Models/aircrftb.mod > Output/aircrftb.out
#
# start awk assigning the value ' to the variable "sq", and the value " to
# the variable "dq". Also, fields are delimited by either "/" or ".".
awk -v sq="'" -v dq='"' -F '[/.]' '{
# NF represents the number of fields in the line (separated by either "/"
# or ".", as said). $NF is the last field; $(NF-1) is the penultimate
# field. With your input, this is the filename we are interested in. Now,
# outputting the extra line you need is just a matter of concatenating
# some strings. In awk, strings can be concatenated just by writing one
# next to another. To avoid awkward notations, we have previously
# assigned the single quote and double quote values to the variables "sq"
# and "dq".
# So, let us print the line (spaced for better readability):
print "printf " sq "%s" sq " " dq " " $(NF-1) " & " dq " >> TABLE.TEX";
# now, print the original line
print
}' yourscript.sh
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Post Follow-up to this message2008-03-25, 19:18(+01), Dieter Kraft:
[...]
> I have a shell script
> with (1000) contiguous textlines like
>
> ampl Models/aircrftb.mod > Output/aircrftb.out
> ampl Models/airport.mod > Output/airport.out
> ampl Models/aljazzaf.mod > Output/aljazzaf.out
> ampl Models/allinitc.mod > Output/allinitc.out
> ampl Models/allinit.mod > Output/allinit.out
> ampl Models/allinitu.mod > Output/allinitu.out
> ampl Models/alsotame.mod > Output/alsotame.out
>
> before each line I want to insert a new line to print
> the model name into a TeX file, like
>
> printf '%s' " aircrftb & " >> TABLE.TEX
(note that as it doesn't contain a newline character, strictly
speaking it's not a line).
> and
> printf '%s' " airport & " >> TABLE.TEX
> etc.
[...]
What about changing your script to:
for mod in Models/*.mod; do
name=${mod#*/}
name=${name%.*}
ampl "$mod" > "Output/$name.out"
printf ' %s & ' "$name" >&3
done 3>> TABLE.TEX
make sure it is interpreted by a POSIX shell (on Solaris, it's
in /usr/xpg4/bin/sh, on most other systems it's in /bin/sh).
--
Stéphane
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.