Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I am sure some of you guys are getting tired of my questions. However, I have yet another one . I promise a drink on me for this one What ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75

    Adding Text

    I am sure some of you guys are getting tired of my questions. However, I have yet another one . I promise a drink on me for this one

    What I am trying to do is make one file full of a list of customer names and their account numbers. Then I will have another file which I will call status that has a bunch of lines in there with various different things. What I want to do is anytime I see an account number in the status file I want to put the customer name in front of it.

    Example of current setup:
    Customer File

    Code:
    R#####1 Customer One
    R#####2 Customer Two
    R#####3 Customer Three
    Status File
    Code:
    R#####1 Some info
    Some info2
    Some info3
    etc
    R#####2 Some info
    some info22
    R#####3 Some info
    Some more info
    some more infox2
    some more infox3

    My desired results
    Code:
    Customer One R#####1 Some info
    Some info2
    some info3
    Customer Two R#####2 Some info
    someinfo222
    Customer Three R#####3 Some info
    Some more info
    some more infox2
    some more infox3
    What I was thinking of doing was making a file full of sed s/R#####1/Customer One R#####1/g I didn't think that would be practical so I came to the best place to ask questions.

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Code:
    % cat customer 
    R#####1 Customer One
    R#####2 Customer Two
    R#####3 Customer Three
    % cat status 
    R#####1 Some info
    Some info2
    Some info3
    etc
    R#####2 Some info
    some info22
    R#####3 Some info
    Some more info
    some more infox2
    some more infox3
    % awk 'NR==FNR{cust[$1]=$2FS$3;next}
    $1 in cust{$0=cust[$1]FS$0}1' customer status
    Customer One R#####1 Some info
    Some info2
    Some info3
    etc
    Customer Two R#####2 Some info
    some info22
    Customer Three R#####3 Some info
    Some more info
    some more infox2
    some more infox3

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    This is not working correctly. It is not replacing data. Could it be because the customers have spaces some times? or there is a space in the customer file from the account R######

  4. #4
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Yes, it doesn't replace anything; it only outputs.
    To change your file:
    Code:
    awk 'NR==FNR{cust[$1]=$2FS$3;next}
    $1 in cust{$0=cust[$1]FS$0}1
    ' customer status>status_new\
    &&mv status status.orig\
    &&mv status_new status
    Last edited by radoulov; 10-23-2007 at 08:36 PM. Reason: corrected

  5. #5
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Quote Originally Posted by Korelis View Post
    This is not working correctly. It is not replacing data. Could it be because the customers have spaces some times? or there is a space in the customer file from the account R######
    Yep,
    it won't work if the R# # # have spaces,
    if you post a realistic sample, it will be easier.

  6. #6
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    You know what my fault for once again not giving a real life example. I figured out why it was not doing it. I had set the example file I used (I cleaned it wierd ly) but the orginial file has a space then * and then space and tab. Looks like

    Code:
     *       R####    Maint   1.0.0.1         Active          Some of the Backup Sets have not run since Oct 20, 2007

  7. #7
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    The corrisponding record in the customer file is R####?

  8. #8
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Correct. The status file is the example I gave in my last post. The customer file looks just as I posted it;

    R##### Customer Name

  9. #9
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    Assuming R##### (5 #) in the customer file and R#### (4 #) in the status file:

    Code:
    awk 'NR==FNR{cust[$1]=$2FS$3;next}
    $2"#" in cust{$0=cust[$2"#"]FS$0}1
    If there are no spaces in the customer name, you can avoid all that $2FS$3,
    it must be only $2.

  10. #10
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    the R###### is actually an R100001 it is an actual number. The status file has the same information they are both 12 characters long. The numbers are different.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...