Monday, September 17, 2007

Delete Tabs, Newlines, Carriage Returns, or Spaces from a File

There are times when you might want to delete formatting characters from a file. The tr command does an excellent job of performing this task. Here are some examples.

Note: I have included a screen shot of this task per the request of a commenter. This example in the jpeg deals with tabs and newlines.

Remove tabs
# more filename
esoft esoft esoft
esoft esoft esoft

# cat filename | tr -d space '\t'
esoftesoftesoft
esoftesoftesoft
# tr -d space '\t' < filename

Remove newlines
# cat filename | tr -d space '\n'
esoft esoft esoftesoft esoft esoft










Additional information:
Remove carriage returns
# cat filename | tr -d space '\r'

Remove spaces
# cat filename | tr -d space "space"


5 comments:

UX-admin said...

Remove CRs:

cat filename | xargs

Not exactly meant for removing CRs, but elegant and effective nevertheless.

esofthub said...

Nice add-on tip ux-admin

Anonymous said...

How could you combine cat filename | xargs with another cat?

ie. cat file1 file2 > file3

where file1 would have it's CR's stripped?

Anonymous said...

The formatting of this post doesn't make the examples entirely clear to me. When I attempt the commands on a test file, I don't see the results (and, yes, I am replacing filename with the name of my file).

I think the issue is spacing between commands and results, et cetera. I'm sure it's all discernible by experienced experts, but it would more clear to everyone if it was more explicit in terms of This Command / This Result.

esofthub said...

anonymous #2:

I have to agree with you and apologize for the inconvenience. I hope the jpeg makes it a bit clearer. I've added some additional explanations to the post, too.

Let me know what you think.