Operating System - HP-UX
1753937 Members
9690 Online
108811 Solutions
New Discussion юеВ

Re: Script challenged: print lines with \ and spaces

 
SOLVED
Go to solution
John J Read
Frequent Advisor

Script challenged: print lines with \ and spaces

I'm just not getting it. I'd appreciate any help. I'm attempting to write a posix script that will convert a file with a few lines into one, comma delimited line so I can load it to a database. The issue is the tricky \ and space characters ending in unwanted escaping, etc.

example:
includes file contains:

D:\AUL Scripts
D:\OVOW_patches
D:\HP OpenView\Data\Databases\backup


I'd like write something in posix that will
cat these and convert to:

D:\AUL Scripts,D:\OVOW_patches,D:\HP OpenView\Data\Databases\backup

thanks! points to everyone!


7 REPLIES 7
Biswajit Tripathy
Honored Contributor

Re: Script challenged: print lines with \ and spaces

I'm sure some one else would give you a
better one, but how about this:

-----------
while read -r line
do
newline="'$line'"
echo $newline,
done < EXAMPLE_FILE | xargs echo | sed 's/, /,/g' | sed 's/,$//g'
--------------

Replace EXAMPLE_FILE by your input file name.

- Biswajit
:-)
A. Clay Stephenson
Acclaimed Contributor

Re: Script challenged: print lines with \ and spaces

Well, if you insist upon an all shell solution (awk or Perl would be easier):

#!/usr/bin/sh

PREV=""
typeset -i KNT=0
while read X
do
if [[ ${KNT} -ne 0 ]]
then
echo "${PREV},\c"
fi
((KNT += 1))
PREV="${X}"
done
echo "${PREV}"


Invoke it like this:

myscript.sh < infile > outfile

If it ain't broke, I can fix that.
John J Read
Frequent Advisor

Re: Script challenged: print lines with \ and spaces

I greatly appreciate your responses but both solutions above miss by a little bit. Also, I'd be happy to try Perl or Awk.

In the first solution, note the /b in the last line ...\backup is being interpreted by the shell as "backup one space". If the file were to begin with \n " interpreted as new-line" then the whole thing fails.

while read -r line
do
newline="'$line'"
echo $newline,
done < ./includes | xargs echo | sed 's/, /,/g' | sed 's/,$//g'

D:\AUL Scripts,D:\OVOW_patches,D:\HP OpenView\Data\Databaseackup <- note the missing "b".


In the second solution the backslashes are lost.

./jj < ./includes
D:AUL Scripts,D:OVOW_patches,D:HP OpenViewDataDatabasesbackup


Feel free to write in alternate languages. It's time I buckle down and get better at Perl. Shell has usually gotten the job done but I always know it would have been easier if I was better at Perl.


Peter Godron
Honored Contributor
Solution

Re: Script challenged: print lines with \ and spaces

John,
a oneliner which should work:

paste -s $filename | sed "1,$ s/ D:/,D:/g" > $newfilename

Please make sure the command is on one line!

The paste -s merges the lines, seperated I believe by tabs, and the sed replaces the tabs with commas.

Please let us know how you get on.
Regards
Peter Godron
Honored Contributor

Re: Script challenged: print lines with \ and spaces

John,
sorry, but perhaps even better ;-)

paste -s -d"," $filename

Regards
John J Read
Frequent Advisor

Re: Script challenged: print lines with \ and spaces

Very cool! Both of the "paste" commands above do the trick! Thanks! This gets me what I need. Shell comes through again!Still going to dedicate some time to Perl.

thanks everyone..
Hein van den Heuvel
Honored Contributor

Re: Script challenged: print lines with \ and spaces


Here are (two of many) perl solution:

perl -e 'while (<>){ chop; $line.=$_."," } chop $line; print "$line\n"' x

D:\AUL Scripts,D:\OVOW_patches,D:\HP OpenView\Data\Databases\backup

- Simple loop over input into default $_
- append each $_ to lien being build, adding a comma.
- chop of final comma and print.


perl -e 'local $/; $_=<>; s/\n/,/g;print' x

This clears a special variable to make perl go into 'slurp' mode reading the whoel file into a string. Now replace every newline with a comma and print.

fwiw,
Hein.