PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > A sed challenge!
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

A sed challenge!

Réponse
 
LinkBack Outils de la discussion
Vieux 02/11/2006, 00h37   #1
David Douthitt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut A sed challenge!

I'm not sure how to go about this in the most effective way, but I'd
thought I'd just throw it out there and see what everyone might come
up with:

The input file is something like this (a fixed size ASCII table:

ESC x x x Description
ESC y y (A) Description2
ESC z (V) Description3
Px = 4 Description5
7 (V) Description6
9 (A) Description7

I'd like to have it come out like this (a LaTeX-formatted table entry):

\verb=ESC x x x= & & Description \\ \hline
\verb=ESC y y= & (A) & Description2 \\ \hline
\verb=ESC z= & (V) & Description3 \\
Px = 4 Description5
7 (V) Description6
9 (A) Description7

....the last few lines will eventually be converted into a
"table-within-a-table". First stab with sed, I would think:

sed '/^ESC/{
s/ *\([VA]\) */ \& \1 \& /;
s/ */ \& \& /;
s/$/ \\\\ \\hline/;
}'

....or did I just solve it? ;-) Bigger challenge: make it come out:

\verb=ESC x x x= & & Description \\ \hline
\verb=ESC y y= & (A) & Description2 \\ \hline
\verb=ESC z= & (V) & Description3 \\
\center
\begin{tabular}{r|c|l}
Px = 4 & & Description5 \\
7 & (V) & Description6 \\
9 & (A) & Description7 \\
\end{tabular}
\end{center} \\

I'm not sure how to make the begin and end (with sed), but since this
is within vi, I can just use a macro to insert them, and do this:

sed '/^ESC/{
s/ *\([VA]\) */ \& \1 \& /;
s/ */ \& \& /;
s/$/ \\\\ \\hline/;
};

/^ /{
s/^ *//;
s/ *\([VA]\) */ \& \1 \& /;
s/ */ \& \& /;
s/$/ \\\\/;
}'

Thoughts? By the way, I know sed a *whole* lot more than LaTeX - so
thoughts on the LaTeX are also welcome...

Thanks! Too bad there's not a comp.lang.sed ;-)

Also, it would seem I can't just use that as given within vim as input
to a !,G command; I'll have to do something else - probably act on a
file and copy it back in...
  Réponse avec citation
Vieux 02/11/2006, 04h43   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A sed challenge!

David Douthitt wrote:
> I'm not sure how to go about this in the most effective way, but I'd
> thought I'd just throw it out there and see what everyone might come up
> with:
>
> The input file is something like this (a fixed size ASCII table:
>
> ESC x x x Description
> ESC y y (A) Description2
> ESC z (V) Description3
> Px = 4 Description5
> 7 (V) Description6
> 9 (A) Description7
>

<snip>
> ...or did I just solve it? ;-) Bigger challenge: make it come out:
>
> \verb=ESC x x x= & & Description \\ \hline
> \verb=ESC y y= & (A) & Description2 \\ \hline
> \verb=ESC z= & (V) & Description3 \\
> \center
> \begin{tabular}{r|c|l}
> Px = 4 & & Description5 \\
> 7 & (V) & Description6 \\
> 9 & (A) & Description7 \\
> \end{tabular}
> \end{center} \\

<snip>

> Thanks! Too bad there's not a comp.lang.sed ;-)


But there is a comp.lang.awk and awk's probably a more appropriate tool
to use to solve this problem.

You don't explain what information you're keying off to make the
conversions so this may not be how you need it to work, but this will
produce the output you want for the given input:

$ cat file
ESC x x x Description
ESC y y (A) Description2
ESC z (V) Description3
Px = 4 Description5
7 (V) Description6
9 (A) Description7

$ awk -f tst.awk file
\verb=ESC x x x= & & Description \\ \hline
\verb=ESC y y= & (A) & Description2 \\ \hline
\verb=ESC z= & (V) & Description3 \\
\center
\begin{tabular}{r|c|l}
Px = 4 & & Description5 \\
7 & (V) & Description6 \\
9 & (A) & Description7 \\
\end{tabular}
\end{center} \\

$ cat tst.awk
FNR==1 { printf "\\verb=%s %s %s %s= & & %s \\\\ \\hline\n",$1,$2,$3,$4,$5}
FNR==2 { printf "\\verb=%s %s %s= & %s & %s \\\\ \\hline\n",$1,$2,$3,$4,$5}
FNR==3 { printf "\\verb=%s %s= & %s & %s \\\\\n",$1,$2,$3,$4}
FNR==4 {
printf "\\center\n"
printf "\\begin{tabular}{r|c|l}\n"
printf "%s %s %s & & %s \\\\\n",$1,$2,$3,$4
}
FNR==5 { printf "%s & %s & %s \\\\\n",$1,$2,$3 }
FNR==6 {
printf "%s & %s & %s \\\\\n",$1,$2,$3
printf "\\end{tabular}\n"
printf "\\end{center} \\\\\n"
}

Regards,

Ed.
  Réponse avec citation
Vieux 02/11/2006, 08h18   #3
John W. Krahn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A sed challenge!

David Douthitt wrote:
> I'm not sure how to go about this in the most effective way, but I'd
> thought I'd just throw it out there and see what everyone might come up
> with:
>
> The input file is something like this (a fixed size ASCII table:
>
> ESC x x x Description
> ESC y y (A) Description2
> ESC z (V) Description3
> Px = 4 Description5
> 7 (V) Description6
> 9 (A) Description7
>
> I'd like to have it come out like this (a LaTeX-formatted table entry):
>
> \verb=ESC x x x= & & Description \\ \hline
> \verb=ESC y y= & (A) & Description2 \\ \hline
> \verb=ESC z= & (V) & Description3 \\
> Px = 4 Description5
> 7 (V) Description6
> 9 (A) Description7


$ echo "ESC x x x Description
ESC y y (A) Description2
ESC z (V) Description3
Px = 4 Description5
7 (V) Description6
9 (A) Description7" | \
perl -lpe'
/^ESC/ and $_ = sprintf q[\verb=%s= & %s & %s \\\%s], unpack( q[A15 A6 A*], $_
), /\(V\)/ ? q[] : q[ \hline]
'
\verb=ESC x x x= & & Description \\ \hline
\verb=ESC y y= & (A) & Description2 \\ \hline
\verb=ESC z= & (V) & Description3 \\
Px = 4 Description5
7 (V) Description6
9 (A) Description7



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h22.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10003 seconds with 11 queries