trying to script a command line

Craig White craig at tobyhouse.com
Wed Mar 5 14:56:57 MST 2008


On Wed, 2008-03-05 at 14:37 -0700, Mike Bydalek wrote:
> Craig White wrote:
> > OK - truly simplifying my issue to a point where someone should be able
> > to explain this to me...
> >
> > # my starting file
> > $ cat test-db.txt
> > A 1
> > B 2
> > C 3
> >
> > # this is what I want
> > $ cat test-db.txt | cat test-db.txt | sed 's/ /\\ /g' | sed ':a;N;$!
> > ba;s/\n/ /g'
> > A\ 1 B\ 2 C\ 3
> >
> > # but if I aggregate the command inside backticks, I lose the 
> > # escaped spaces part of the sed command...
> >
> > $ TEST=`cat test-db.txt \
> >   | cat test-db.txt \
> >   | sed 's/ /\\ /g' \
> >   | sed ':a;N;$!ba;s/\n/ /g'`
> >
> > $ echo $TEST
> > A 1 B 2 C 3
> >
> > So the carriage returns are removed by the spaces aren't escaped.
> >
> > How can I do this so I can aggregate a file with fields on each line so
> > that the carriage returns are removed and the spaces are escaped?
> >
> >   
> $ TEST=`cat test-db.txt | sed 's/ /\\\ /g' | tr '\n' '\\ '`
> $ echo $TEST
> A\ 1 B\ 2 C\ 3
> 
> One thing I don't get is, why use cat twice?  Also, imo tr is better 
> than sed at replacing '\n' since what you have is gnu specific.
----
the 2 'cat' commands was from a paste over a paste...sorry.

as I said in response to Charles, the third backslash was a key
component. Your tr suggestion makes sense and I'm a bash nightmare so
thanks.

pdftk is not the easiest to script and while the command line and
scripts output as intended and should be sufficient for pdftk, it still
breaks on command but the shell script is no longer the issue.

Thanks

Craig



More information about the PLUG-discuss mailing list