Grabbing text and using that text in a newly created line

Hello,

I am really stuck and I'm hoping somone can help. I have a text file that is similar to this:

<--First User-->
<function>account='uid=user1,.......
 
<--Second User-->
<function>account='uid=user2,.......

What I want is to grab the usernames after "uid=" and before the following comma. I cannot figure this out.

I then need to add a new line to each user's section. This new line is the same for all users, with the exception of needing the specific username for that user. For example I would like it to do this:

<--First User-->
<function>account='uid=user1,.......
<newlycreatedline>hello my name is user1......
 
<--Second User-->
<function>account='uid=user2,.......
<newlycreatedline>hello my name is user2......

Anyone know how to script this out, I need this done for too many users to do it by hand.

nawk -F'[=,]' '/uid=/{print $0 ORS "hello my name is " $3;next}1'  myFile
1 Like

Is this what the data actually looks like? The usual problem with XML is "whoops, I forgot to mention the data's full of whitespace and not one record per line because it looked bad".

Corona you're right, here is a more accurate depiction of what it looks like:

<info accountId='uid=user1,ou=example,dc=example,dc=com'>
      <object type='examp' id=123456 name='EX'/>
   </info>
   <newlineneedhere accountId='user1' GUID='id=user1,dc=example,dc=com/>

Where the white space in front counts. 3 spaces for most 6 spaces for the object. I need the username grabbed to be in the two spots on the newly created line.

Thanks!

Hi mafia910,

Using 'sed':

$ cat infile
<--First User-->
<function>account='uid=user1,.......

<--Second User-->
<function>account='uid=user2,.......
$ sed -n '/uid=\([^,]*\)/ { p; s/^.*uid=\([^,]*\).*$/\1/; h; s/^.*$/<newlycreatedline>hello my name is /; G; s/\n//; p; n }; p' infile
<--First User-->
<function>account='uid=user1,.......
<newlycreatedline>hello my name is user1

<--Second User-->
<function>account='uid=user2,.......
<newlycreatedline>hello my name is user2

Regards,
Birei

vgersh that works for my original example, thanks! Now I just need to adapt it to the actual data structure. If you are able to look at that in my previous reply, that would be awesome. Either way, big help!

---------- Post updated at 12:02 PM ---------- Previous update was at 11:52 AM ----------

I guess my main question now is, how do I print the username inside of single quotes. For instance, instead of:

hello my name is user1

I need:

hello my name is 'user1'

Please post the exact input AND the exact desired output. It's hard to keep guessing.

Sorry for causing confusion. The exact input and out is my response to Corona:

<info accountId='uid=user1,ou=example,dc=example,dc=com'>
      <object type='examp' id=123456 name='EX'/>
   </info>
   <newlineneedhere accountId='user1' GUID='id=user1,dc=example,dc=com/>

I need the usernames to appear in the two spots in <newlineneedhere, between the single quotes.

a lil' long, but....

nawk -F'[=,]' -v q="'" -v s='/' '/<info.*uid=/{u=$3;id=substr($0,index($0,q)+1)}$0 ~("<" s "info>"){print $0 ORS "<newlineneedhere accountId=" q u q OFS "GUID=" q substr(id,1,length(id)-2) q;next}1' myFile

That worked perfectly! Thanks again!