LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2021, 02:50 PM   #1
__John_L.
LQ Newbie
 
Registered: Feb 2021
Posts: 14

Rep: Reputation: Disabled
sed command to collapse whitespace


I'm trying to write a sed command to collapse white space (tab, newlines, spaces, etc.) and whittle consecutive characters down to one space.

I tried:

Code:
sed "s/\s+/ /gm" infile.txt >outfile.txt
and also:

Code:
sed "s/\s{1,}/ /gm" infile.txt >outfile.txt
but both have no effect on whitespace in the input. So much for walking with sed.

Thanks in advance for any help you can provide.
 
Old 02-20-2021, 02:57 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
+ is a meta-character in extended regular expressions only. But sed uses basic regular expressions by default.
Code:
sed 's/\s\+/ /gm' infile.txt >outfile.txt
or
Code:
sed -r 's/\s+/ /gm' infile.txt >outfile.txt
or
Code:
sed -E 's/\s+/ /gm' infile.txt >outfile.txt
But they all won't match across the lines despite the m flag. For this, I'd rather
Code:
tr -s '[:space:]' ' ' <infile.txt >outfile.txt

Last edited by shruggy; 02-20-2021 at 03:41 PM.
 
3 members found this post helpful.
Old 02-20-2021, 05:30 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,152

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Be careful to define exactly what you want. No indented lines you want to retain - paragraph or lists for example ?. Newlines ?. Why keep a space at eol ?.
 
1 members found this post helpful.
Old 02-21-2021, 01:22 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
Standard/Posix sed (portable):
Code:
sed "s/[[:space:]]\{1,\}/ /g"
 
1 members found this post helpful.
  


Reply

Tags
sed regex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed - substituting multiple occurrances of whitespace with a ':' simplified Linux - Newbie 8 12-05-2009 03:38 AM
sed whitespace substitution problem. arashi256 Linux - Newbie 18 07-17-2009 11:03 AM
sed, replacing underscore with whitespace fjkum Programming 3 10-31-2007 12:09 AM
Whitespace parsing sed? carl.waldbieser Programming 1 12-12-2005 04:24 PM
Using sed in bash to remove whitespace jimieee Programming 3 01-28-2004 10:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration