LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-14-2010, 09:41 AM   #1
CincinnatiKid
Member
 
Registered: Jul 2010
Posts: 454

Rep: Reputation: 47
Bash Command to Remove Spaces


*I meant to post this in Linux General, can one of the moderators move it?*

I am reading the output of /proc/acpi/thermal_zone/ATF0/temperature in a program to read my CPU temp. I am using cat like the following:

Code:
#cat /proc/acpi/thermal_zone/ATF0/temperature
temperature:                       49 C
I basically want to get rid of the spaces in between temperature and the actual temperature. Is there a command I can pipe the cat output to, to remove the spaces. I have seen suggestions for sed, or tr, but for some reason I cannot get them to work properly.

Last edited by CincinnatiKid; 09-14-2010 at 09:44 AM. Reason: Wrong Forum
 
Old 09-14-2010, 09:44 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The following removes all spaces:
sed 's/ [ ]*//' /proc/acpi/thermal_zone/ATF0/temperature

If you want to leave one space:
sed 's/ [ ]*/ /' /proc/acpi/thermal_zone/ATF0/temperature

Hope this helps.
 
Old 09-14-2010, 09:56 AM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
ruby -ne  'gsub(/  +/,"");print $_' /proc/acpi/thermal_zone/ATF0/temperature
 
Old 09-14-2010, 09:59 AM   #4
CincinnatiKid
Member
 
Registered: Jul 2010
Posts: 454

Original Poster
Rep: Reputation: 47
I used the sed method and it worked great. Thanks.
 
Old 09-14-2010, 10:03 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
Old 09-14-2010, 10:57 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Here's what I would use:

Code:
cat /proc/acpi/thermal_zone/ATF0/temperature | tr -d [:space:]
That sed command cannot handle anything but spaces, so it won't handle tabs or newlines (if they are in there). If there are none then it will work ok.
 
Old 09-14-2010, 11:17 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The OP talked about spaces and a specific scenario......

Here's one that does all white space and multiple instances on a line:

sed 's/[[:space:]][[:space:]]*/ /g' infile

Always better to use one command instead of two and a pipe
 
Old 09-14-2010, 11:39 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
that works, and there could have been a tab in there, how could you know ?
 
Old 09-14-2010, 01:16 PM   #9
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
You said bash right? forget tr, sed & Co.
Code:
while read line ; do
	echo $line
done < /proc/acpi/thermal_zone/ATF0/temperature
 
Old 09-14-2010, 01:38 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@gnashley:
 
Old 09-18-2010, 05:56 AM   #11
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by lewisforlife View Post
*I meant to post this in Linux General, can one of the moderators move it?*
In future, please use the "Report" button (bottom right of the post) to ask the moderators to move threads. Doing so is guaranteed to get their attention (I believe an email is sent when posts are reported), but posting in your thread is not, because they aren't necessarily going to read it (whatever the reason may be - they can't look at every single thread, for example).

Having said that, I've reported it for you now. It's probably better off in Programming, but obviously the mods will make that decision.
 
Old 09-18-2010, 08:03 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by druuna View Post
Hi,

The following removes all spaces:
sed 's/ [ ]*//' /proc/acpi/thermal_zone/ATF0/temperature

If you want to leave one space:
sed 's/ [ ]*/ /' /proc/acpi/thermal_zone/ATF0/temperature

Hope this helps.
What's the reason for the square brackets if there's one character inside? Just a space would work the same.

Also, why look for a space followed by 0 or more spaces, when you can use the + operator which means one or more?

Last edited by MTK358; 09-18-2010 at 08:05 AM.
 
Old 09-18-2010, 08:43 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@MTK358:
Quote:
What's the reason for the square brackets if there's one character inside?
Habit, I automatically use space and tab when hunting for whitespace (I should use :space: more often) and forget that the square brackets aren't really needed when removing either of the two. I don't use the + that much because in some programs it needs to be escaped (sed being an example).

This would indeed work:
Code:
sed 's/  */ /' /proc/acpi/thermal_zone/ATF0/temperature
This would not:
Code:
sed 's/ */ /' /proc/acpi/thermal_zone/ATF0/temperature
But this would:
Code:
sed 's/ \+/ /' /proc/acpi/thermal_zone/ATF0/temperature
 
Old 09-18-2010, 08:53 AM   #14
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by druuna View Post
I don't use the + that much because in some programs it needs to be escaped[INDENT](sed being an example).
sed -r ?
 
Old 09-18-2010, 08:58 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by kurumi View Post
sed -r ?
Yes, that would also work in sed's case. Like I stated already: Habit.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash command needed to remove failed install nnjond Linux - Newbie 1 05-12-2010 06:54 AM
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
Execute command with spaces from variable in bash script klo_2k Linux - Newbie 4 04-13-2008 02:59 AM
remove all spaces from a line in C xeon123 Programming 12 10-23-2007 01:29 AM
Is it possible to remove spaces Alex_jacobson Linux - General 4 01-16-2005 10:45 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:09 PM.

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