Grabbing text and using that text in a newly created line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grabbing text and using that text in a newly created line
# 1  
Old 10-18-2011
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:

Code:
<--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:

Code:
<--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.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-18-2011 at 12:40 PM.. Reason: code tags please!
# 2  
Old 10-18-2011
Code:
nawk -F'[=,]' '/uid=/{print $0 ORS "hello my name is " $3;next}1'  myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 10-18-2011
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".
# 4  
Old 10-18-2011
Corona you're right, here is a more accurate depiction of what it looks like:

Code:
<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!

Last edited by vgersh99; 10-18-2011 at 12:52 PM.. Reason: code tags, please!
# 5  
Old 10-18-2011
Hi mafia910,

Using 'sed':
Code:
$ 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

Last edited by birei; 10-18-2011 at 12:59 PM.. Reason: Remove my code for mafia910's second example. It doesn't work.
# 6  
Old 10-18-2011
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'
# 7  
Old 10-18-2011
Please post the exact input AND the exact desired output. It's hard to keep guessing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grabbing text between two lines with shell variables.

I would like to grab complex html text between lines using variables. I am running Debian and using mksh shell. Here is the part of the html that I want to extract from. I would like to extract the words 'to love,' and I would like to use the above and below lines as reference points. ... (3 Replies)
Discussion started by: bedtime
3 Replies

2. Shell Programming and Scripting

Grabbing data between 2 points in text file

I have a text file that shows the output of my solar inverters. I want to separate this into sections. overview , device 1 , device 2 , device 3. Each device has different number of lines. but they all have unique starting points. Overview starts with 6 #'s, Devices have 4#'s and their data starts... (6 Replies)
Discussion started by: Mikey
6 Replies

3. Hardware

Formatting a newly created lun

Hi , I have created one new lun in my SAN storage and make it visible to my HP servers , but the fdisk -l output is somehow confusing. Do not know what to do next ---------- fdisk -l /dev/sdo1 Disk /dev/sdo1 (Sun disk label): 64 heads, 32 sectors, 10238 cylinders Units =... (7 Replies)
Discussion started by: mishra.sankar
7 Replies

4. Shell Programming and Scripting

Grabbing a chunk of text from a file

Hi, I have a Report.txt file. Say the contents of this file are : 1 2 3 4 5 7 df v g gf e r dfkf lsdk dslsdklsdk Report Start: xxxxxxdad asdffsdfsdfsdfasfasdffasdf sadfasdfsadffsfsdf Report End. sdfasdfasdf sdfasfdasdfasdfasdfasdf sadfasdfsdf I need to grab from Report Start... (3 Replies)
Discussion started by: mrskittles99
3 Replies

5. Shell Programming and Scripting

Grabbing top line of text in a file and setting it to a variable

If I have a txt file with test.txt somelineoftext and I want to set that line of text to variable in a script: so #!/bin/bash var='' becomes #!/bin/bash var='somelineoftext' (3 Replies)
Discussion started by: digitalviking
3 Replies

6. Shell Programming and Scripting

How to find the newly created directory

Hi, I need to create new directory by increasing the number by 1 of extracted lastly created directory. e.g. Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ become Log\out_log_1\ Log\out_log_2\ Log\out_log_3\ Log\out_log_4\ Can anyone help how to do it in c-shell... (3 Replies)
Discussion started by: Andre_2008
3 Replies

7. Shell Programming and Scripting

grabbing filename from text file....should be easy!

Quick question...I'm trying to grab the .tif file name from this output from our fax server. What is the best way i can do this in a bash script? I have been looking at regular expressions with bash or using awk but having some trouble. thanks! The only output i want is... (5 Replies)
Discussion started by: kuliksco
5 Replies

8. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

9. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

10. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies
Login or Register to Ask a Question