LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Regexp problem (http://www.linuxquestions.org/questions/programming-9/regexp-problem-905720/)

jawsnnn 09-29-2011 11:45 PM

Regexp problem
 
Here is the problem:

I have a file with some strings separated by spaces, and with delimiters, e.g.


1 2 3 4 5 ~1 2 3 4 5 ~
1 2 3 4 5 ~1 2 3 4 5 ~
1 2 3 4 5 ~1 2 3 4 5 ~


I want to replace the spaces between the field and the delimiters to some character (@ in this example)


1 2 3 4 5@@@@~1 2 3 4 5@@@~
1 2 3 4 5@@@@~1 2 3 4 5@@@~
1 2 3 4 5@@@@~1 2 3 4 5@@@~

The regexes I have tried so far are obviously wrong:

sed 's/[ ]*~/@/g' # will replace all spaces with a single @
sed 's/[ ]/@/g' # will replace all spaces, even those within data with a # single @

Any ideas?

grail 09-30-2011 01:11 AM

Please explain further what a field is?

crts 09-30-2011 02:22 AM

Quote:

Originally Posted by jawsnnn (Post 4486137)
Here is the problem:

I have a file with some strings separated by spaces, and with delimiters, e.g.


1 2 3 4 5 ~1 2 3 4 5 ~
1 2 3 4 5 ~1 2 3 4 5 ~
1 2 3 4 5 ~1 2 3 4 5 ~


I want to replace the spaces between the field and the delimiters to some character (@ in this example)


1 2 3 4 5@@@@~1 2 3 4 5@@@~
1 2 3 4 5@@@@~1 2 3 4 5@@@~
1 2 3 4 5@@@@~1 2 3 4 5@@@~

The regexes I have tried so far are obviously wrong:

sed 's/[ ]*~/@/g' # will replace all spaces with a single @
sed 's/[ ]/@/g' # will replace all spaces, even those within data with a # single @

Any ideas?

Hi,

please use code-tags in the future so that we can see how many spaces there are in the sample data.

As for your problem, try this:
Code:

sed -r ':a s/ (@*~)/@\1/g;ta' file
Requires GNU sed.


All times are GMT -5. The time now is 08:56 PM.