LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   removing white spaces in the file (http://www.linuxquestions.org/questions/linux-newbie-8/removing-white-spaces-in-the-file-693785/)

tucs_123 12-30-2008 01:19 AM

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...........

rhoekstra 12-30-2008 01:43 AM

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'

tucs_123 12-30-2008 02:58 AM

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

rhoekstra 12-30-2008 03:01 AM

Quote:

Originally Posted by tucs_123 (Post 3391265)
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'

tucs_123 12-30-2008 03:27 AM

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?

sycamorex 12-30-2008 03:40 AM

Code:

cat 2.txt | sed 's/[ \t]\+/,/g'
What about this one? It should replace both blank spaces and tabs with commas.

sycamorex 12-30-2008 03:43 AM

sorry, post made by mistake

rhoekstra 12-31-2008 10:45 AM

What you could do is this:
Code:

cat 2.txt | sed -e 's/\t*/,/g' -e 's/ ,/,/g'
Perhaps that helps?

pixellany 12-31-2008 02:43 PM

Code:

sed 's/[[:blank:]]\+/XX/g'
replaces a pattern with at least one "whitespace" character---eg tabs, spaces.

archtoad6 01-03-2009 11:28 AM

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, RT:)M . . . regularly.


All times are GMT -5. The time now is 09:46 AM.