Solaris remove empty space from text file lines

I have a text file in Solaris 10. Sample lines of the text file is attached. It is comma separated line (two field in each line separated by a comma).

But each line's field contents have large number of leading/trailing empty spaces. I could use command to delete empty line:
perl -ni -e 'print if /\S/' /home/.../myfile.txt

How can I write a command that will delete all the leading/trailing blank spaces in each field?  sample.txt
toookiAsked:
Who is Participating?
 
SuperdaveCommented:
Okay...
perl -ni -e 's/ *,/,/; s/ *$//; print if /\S/' sample.txt

A little simplistic--if the real data contain commas in quotation marks it might need enhancing.
0
 
SuperdaveCommented:
perl -ni -e 's/^ *//; s/ *$//; print if /\S/'   /home/.../myfile.txt
0
 
wesly_chenCommented:
sed s/"\s"//g myfile.txt > myfile1.txt
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
SuperdaveCommented:
Wait, I was too quick and didn't read the sample data; ignore that and I'll try again...
0
 
toookiAuthor Commented:
Thank you all.
perl -ni -e 's/ *,/,/; s/ *$//; print if /\S/' sample.txt
The above query mostly works for me.
Could you Superdave, please explain a line about how the command works.?
0
 
SuperdaveCommented:
I started with what you had, but before printing the line, modified it with the two substitution commands.

s/ *,/,/

means replace any number of spaces followed by a comma, with a comma.  (* means any number of the preceding thing.)

s/ *$//

means replace any number of spaces up to end-of-line with nothing.  ($ means end of line).

Semi-colon separates commands.
0
 
toookiAuthor Commented:
Thank you very much. I understand now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.