- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script challenged: print lines with \ and spac...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2005 12:19 PM
тАО01-25-2005 12:19 PM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2005 01:03 PM
тАО01-25-2005 01:03 PM
Re: Script challenged: print lines with \ and spaces
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
- Tags:
- read
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2005 01:10 PM
тАО01-25-2005 01:10 PM
Re: Script challenged: print lines with \ and spaces
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2005 02:47 PM
тАО01-25-2005 02:47 PM
Re: Script challenged: print lines with \ and spaces
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2005 08:50 PM
тАО01-25-2005 08:50 PM
Solutiona 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-25-2005 08:55 PM
тАО01-25-2005 08:55 PM
Re: Script challenged: print lines with \ and spaces
sorry, but perhaps even better ;-)
paste -s -d"," $filename
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2005 01:23 AM
тАО01-26-2005 01:23 AM
Re: Script challenged: print lines with \ and spaces
thanks everyone..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-26-2005 01:51 AM
тАО01-26-2005 01:51 AM
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.
- Tags:
- Perl