LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-30-2008, 12:19 AM   #1
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Rep: Reputation: 15
removing white spaces in the file


Hi,

Can any one tell me how to remove white spaces from start of the line and in between the words in the file using unix commands?

My actual data in the text file is as below with spaces.The spaces are not being shown in the post..please see them while replying to the post.

Dec 22 2008 8:30PM Dec 22 2008 8:55PM Dec 23 2008 12:00AM Dec 23 2008 1:44AM Dec 23 2008 1:44AM



I want output in a single line as below removing white spaces in the file:

Dec 22 2008 8:30PM,Dec 22 2008 8:55PM,Dec 23 2008 12:00AM,Dec 23 2008 1:44AM,Dec 23 2008 1:44AM


Thanks in advance...........

Last edited by tucs_123; 12-30-2008 at 12:23 AM.
 
Old 12-30-2008, 12:43 AM   #2
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 354

Rep: Reputation: 40
The thing is that you probably want to replace 'more than one spaces' with a comma, as you suggest in your post.. right?

so what you could do is trying to cat the file and with sed do a search-and-replace for 'more than one space' to a comma.

something like
Code:
$ cat file.txt|sed 's/  \+/,/g'
 
Old 12-30-2008, 01:58 AM   #3
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
hey thanks for the reply,but its not working for me......


cat 2.txt|sed 's/ \+/,/g'
Dec 22 2008 8:30PM Dec 22 2008 8:55PM Dec 23 2008 12:00AM Dec 23 2008 1:44AM Dec 23 2008 1:44AM
 
Old 12-30-2008, 02:01 AM   #4
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 354

Rep: Reputation: 40
Quote:
Originally Posted by tucs_123 View Post
hey thanks for the reply,but its not working for me......


cat 2.txt|sed 's/ \+/,/g'
Dec 22 2008 8:30PM Dec 22 2008 8:55PM Dec 23 2008 12:00AM Dec 23 2008 1:44AM Dec 23 2008 1:44AM
Are you sure those are spaces and not tabs? in that case you could try:
cat 2.txt|sed 's/\t\+/,/g'
 
Old 12-30-2008, 02:27 AM   #5
tucs_123
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Original Poster
Rep: Reputation: 15
hey,I think those are tabs.but your command didnt work for me.

When I use,

cat 2.txt|sed 's/[\t ][\t ][\t ][\t ][\t ]/,/g'
, Dec 22 2008 8:30PM , Dec 22 2008 8:55PM , Dec 23 2008 12:00AM , Dec 23 2008 1:44AM , Dec 23 2008 1:44AM

it works for me,but still I want to remove blank space between say 8:30PM and comma..how to do that?

Last edited by tucs_123; 12-30-2008 at 02:31 AM.
 
Old 12-30-2008, 02:40 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,564
Blog Entries: 1

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
Code:
cat 2.txt | sed 's/[ \t]\+/,/g'
What about this one? It should replace both blank spaces and tabs with commas.
 
Old 12-30-2008, 02:43 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,564
Blog Entries: 1

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
sorry, post made by mistake
 
Old 12-31-2008, 09:45 AM   #8
rhoekstra
Member
 
Registered: Aug 2004
Location: The Netherlands
Distribution: RedHat 2, 3, 4, 5, Fedora, SuSE, Gentoo
Posts: 354

Rep: Reputation: 40
What you could do is this:
Code:
cat 2.txt | sed -e 's/\t*/,/g' -e 's/ ,/,/g'
Perhaps that helps?
 
Old 12-31-2008, 01:43 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Code:
sed 's/[[:blank:]]\+/XX/g'
replaces a pattern with at least one "whitespace" character---eg tabs, spaces.
 
Old 01-03-2009, 10:28 AM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
2 quick points about sed:
  1. It has a "-r" option that allows you to use extended regular expressions in your scrip script, i.e. no need to escape '+', '{', '}', etc.

  2. '/' is not the only character that can be used to punctuate a substitute command, sed will use the 1st character after the "s" as the separator.

    Why should we care? Visual distraction, compare:
    Code:
    sed 's/\/\/\/\//\/\//'
    with
    Code:
    sed s,////,//'

Thus,
Code:
sed -r 's,[[:blank:]]+,XX,g'
or possibly:
Code:
sed -r 's![[:blank:]]+!,!g'

As always when dealing w/ CLI utilities, RTM . . . regularly.
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
omit white spaces for pattern matching in shell script bharatbsharma Programming 4 09-18-2008 08:25 PM
regular expressions ~ white spaces stevie_velvet Programming 3 07-15-2006 10:28 AM
problems in removing white spaces from string of text monil Programming 7 03-08-2005 11:28 AM
how to get rid of the input beginning white spaces feetyouwell Programming 3 09-30-2004 01:33 AM
Remove white spaces in HTML files using shell : Possible???????? cpanelskindepot Programming 2 08-05-2004 11:14 PM


All times are GMT -5. The time now is 11:55 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration