LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-29-2011, 11:45 PM   #1
jawsnnn
LQ Newbie
 
Registered: Sep 2011
Posts: 4

Rep: Reputation: Disabled
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?
 
Old 09-30-2011, 01:11 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,424

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Please explain further what a field is?
 
Old 09-30-2011, 02:22 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by jawsnnn View Post
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.
 
  


Reply

Tags
regex, 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
Problem with sed regexp Chipper Linux - General 12 03-19-2011 05:49 AM
Problem with regexp in c alitrix Programming 12 09-12-2007 10:15 AM
Regexp problem eremit Programming 6 06-23-2005 06:48 AM
PHP regexp problem abdul_zu Linux - Software 2 04-03-2005 07:35 AM
perl regexp problem raven Programming 4 03-21-2004 11:49 PM


All times are GMT -5. The time now is 02:56 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