Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

insert blank line
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

Report this thread to moderator Post Follow-up to this message
Old Post
Dieter Kraft
03-26-08 12:00 AM


Re: insert blank line
Dieter 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.

Report this thread to moderator Post Follow-up to this message
Old Post
pk
03-26-08 12:00 AM


Re: insert blank line
pk 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

Report this thread to moderator Post Follow-up to this message
Old Post
Dieter Kraft
03-26-08 12:00 AM


Re: insert blank line
Dieter 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.

Report this thread to moderator Post Follow-up to this message
Old Post
pk
03-26-08 12:00 AM


Re: insert blank line
2008-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

Report this thread to moderator Post Follow-up to this message
Old Post
Stephane CHAZELAS
03-26-08 12:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:45 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.