Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Comments on Simplest way of stripping leading/trailing whitespace from file or program output

Parent

Simplest way of stripping leading/trailing whitespace from file or program output

+4
−1

What is the simplest shell idiom for stripping leading and trailing whitespace from a file or program output? Ideally I am looking for the equivalent of trim or strip methods in some languages.

The ideal solution should

  • skip empty lines at the beginning and end of the file/stream
  • provide an option to also strip leading and trailing whitespace from all non-empty lines
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Do tab spaces count as leading or trailing white space? (2 comments)
Post
+4
−0

The simple and obvious solution:

sed 's/^ *//;s/ *$//'

Many recipes you find online will erroneously add a g flag, but these regular expressions can only match once per line anyway.

(In some more detail, s/from/to/g says to replace all occurrences of from on the current input line; but of course, if you know from can only match once, you don't want or need that. It is harmless as such, of course, but betrays a cargo cultish lack of understanding of the construct.)

Your requirement to treat the first and last lines differently seems odd to me, but sed easily allows you to do that too.

sed '1s/^ *//;$s/ *$//'

This adds the address expression 1 to the first command (which matches on line number 1) and the address $ to the last (which matches the final input line).

If your sed implementation doesn't support stringing multiple commands together with ; as shown above, you can pass in the script piecemeal with multiple -e options.

sed -e '1s/^  *//' -e '$s/ *$//'

The regular expressions above specifically target literal spaces. If you want to target any whitespace, replace each with [[:space:]], which is a POSIX character class which matches one whitespace character of any kind (space, tab, etc).

Something similar could be achieved with Awk with a clever RS (record separator) but I'd consider that more obscure, as well as probably slower.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

The OP mentioned whitespace, but this will only remove spaces. Also, you have the portability backwar... (3 comments)
The OP mentioned whitespace, but this will only remove spaces. Also, you have the portability backwar...
terdon‭ wrote 10 months ago

The OP mentioned whitespace, but this will only remove spaces. Also, you have the portability backwards, -e should be supported by most (all, I think, pretty sure that's POSIX) sed implementations, it's passing multiple commands in a single call (sed 'foo; bar') that isn't portable. GNU sed can do it, but many others cannot. The portable way is using multiple -e calls.

tripleee‭ wrote 10 months ago

Thanks, I have updated this to suggest [[:space:]] instead. The -e stuff is based on my experience with MacOS sed, which is sometimes wacky with -e. (Not at my computer so can't provide a specific demo.)

terdon‭ wrote 10 months ago

Ah, OK, I haven't used BSD sed (which is the one that ships with macOS, I think) much, but I can confirm that supporting multiple -e is indeed part of the POSIX specs so that'll be the most portable option. Kudos for pointing out the uselessness of the g flag in cases like these, by the way, that's one of my personal bugbears :)