LinuxQuestions.org
Visit Jeremy's Blog.
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 08-18-2008, 07:11 AM   #1
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Rep: Reputation: 30
sed 1st word to replace all instances of (


I'd like to use sed to take something like:
[("TOWER", "JOHN JERRY","93") (at the beginning of the line) and replace every succeeding ( with it.
(from [("TOWER", "JOHN JERRY","93"), (a), (b), (c)]
(I'm having a bit of a time with back references and .* and \{1\}

Problems:
how do I limit to just the first ) to define my back reference?
(Otherwise ".*)" fills up to the last ")" but I'm only interested in taking the info from the first ).

Each line has a name and several (.
I want to add the name in front of each (. I can use a back reference \1
to edit back references sed 's/\([0-9][0-9]\))/"\1")/g' but how do I copy the /1 back reference to multiple places?
 
Old 08-18-2008, 08:41 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
It is not completely clear what you are trying to do. In SED, a backreference typically works inside the "s" command, and I'm not sure it will do what you are saying.

Easy questions:
.* means "any number of characters" and is "greedy"--ie it will match the longest string it can, so ".*)" goes to the very last ")".

\{1\} is the notation for the number of times to match a regex--not clear how this applies here.

Perhaps you can post a few lines from the file, and a sample of what you want the output to look like. Also, have you looked at other utilites--eg AWK?
 
Old 08-18-2008, 05:42 PM   #3
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by pixellany View Post
It is not completely clear what you are trying to do. In SED, a backreference typically works inside the "s" command, and I'm not sure it will do what you are saying.
I'm not sure either. I just thought it might. I know you can flip things around with a back reference; so I thought I might be able to do that.

Quote:
Originally Posted by pixellany View Post
Easy questions:
.* means "any number of characters" and is "greedy"--ie it will match the longest string it can, so ".*)" goes to the very last ")".
Right. What I was wondering is what is the non-greedy form?


Quote:
Originally Posted by pixellany View Post
\{1\} is the notation for the number of times to match a regex--not clear how this applies here.
Sorry, I was thinking along the lines of [^A] syntax (or \[^\A\] ) to limit the regex to the first appearance of the term. (Here A but 'I'm looking to limit to the first ")".

Quote:
Originally Posted by pixellany View Post
Perhaps you can post a few lines from the file, and a sample of what you want the output to look like. Also, have you looked at other utilites--eg AWK?
The input is basically:

[("TOWER", "JOHN","12"),("AG MECHANICS I","1","A","123","316","SISAK"),("AG MECHANICS I","1","A","456","316","SISAK"),("ENGLISH I","2","A","123","316","SMITH")]

and I would like the output to be:
[("TOWER", "JOHN","12","AG MECHANICS I","1","A","123","316","SISAK"),("TOWER", "JOHN","12","AG MECHANICS I","1","A","456","316","SISAK")("TOWER", "JOHN","12","ENGLISH I","2","A","123","316","SMITH")]

I was thinking of looking at awk or tr.
 
Old 08-18-2008, 06:48 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
An awk solution:
Code:
{
 string = substr($0,1,index($0,")")-1)
 header = substr(string,index(string,"(")+1)
 len = split($0,array,/\),\(/)
 printf "["
 for ( i = 2; i <= len; i++ )
    if ( i == len )
       printf "(%s,%s\n",header,array[i]
    else
       printf "(%s,%s),",header,array[i]
}
The function index returns the position of the first occurrence of the searched string, so that you can extract the substring before the first ")". After that you strip out the leading "[(" and obtain the string (header) to repeat after each "(". Then you simply split the whole line using "),(" as separator and print array elements accordingly.
 
Old 08-18-2008, 07:24 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
With sed:-
Code:
a='(\([^)]*\))'
sed "s/^\[$a,$a,$a,$a\]/[(\1,\2),(\1,\3),(\1,\4)]/" infile > outfile
The variable 'a' is used just to make the structure of the sed statement clearer.
 
Old 08-21-2008, 06:43 PM   #6
donnied
Member
 
Registered: Oct 2006
Distribution: Debian x64
Posts: 198

Original Poster
Rep: Reputation: 30
I ended up use a quick awk solution. I used sed to change all the () to | as my delimiters and with awk I:

print { $1 $2 }
print { $1 $3 }
...
print { $1 $40 }

Thank you for the suggestions.
 
  


Reply

Tags
reference, 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
need sed help - how to replace all instances of X except those on lines with Y? BrianK Programming 4 03-25-2008 06:49 PM
file renaming question--replace multiple instances David the H. Linux - General 4 01-01-2008 12:05 AM
replace word in a file ust Linux - Software 2 11-27-2007 08:39 PM
find and replace answer word? matarodi Linux - General 0 10-27-2005 01:51 PM
Replace word in VIM ICO Linux - Software 2 03-17-2004 01:28 AM


All times are GMT -5. The time now is 07:53 PM.

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