ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
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?
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
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
\{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
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?
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")]
{
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.