Link to home
Start Free TrialLog in
Avatar of toooki
toooki

asked on

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
Avatar of Superdave
Superdave
Flag of United States of America image

perl -ni -e 's/^ *//; s/ *$//; print if /\S/'   /home/.../myfile.txt
sed s/"\s"//g myfile.txt > myfile1.txt
Wait, I was too quick and didn't read the sample data; ignore that and I'll try again...
ASKER CERTIFIED SOLUTION
Avatar of Superdave
Superdave
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toooki
toooki

ASKER

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.?
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.
Avatar of toooki

ASKER

Thank you very much. I understand now.