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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.?
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.
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.
ASKER
Thank you very much. I understand now.