Results 1 to 3 of 3

Thread: Search contents in a file and create files accordingly

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Search contents in a file and create files accordingly

    Hi Experts,

    I have a problem regarding the file. I want the script read the file and make files according to the contents. Will this be possible? Below is the sample of my file.
    Code:
    abcATabcDOTcom                 abccom
    pqrATabcDOTcom                  pqrabc
    godATabcDOTcom                 ghoabc
    appleATfruitsDOTcom             applefruits
    orangeATfruitsDOTcom           orangefruits
    guavaATfruistDOTcom             guavafruits
    laptopATcomputerDOTcom      lapcomputer
    mouseATcomputerDOTcom     moucomputer
    god1ATabDOTcom                  gh1oabc
    hddATcomputerDOTcom         hddcomputer
    pplATnixcraftDOTcom             pplnixcraft
    adminATnixcraftDOTcom        admnixcraft
    accountsATnixcraftDOTcom    accountnixcraft
    My concern is that, read this file and redirect the contents only of abc.com, fruits.com and so on with file name abc.com, fruits.com and so on. Will this be possible in scripting. I am running CentOS. It would have been easier if the file was of fewer line, the file is more than 3000 lines with various domains.

    Any helps will be highly appreciated.

    Thanks
    Franklin
    Last edited by nixcraft; 26th December 2012 at 11:56 PM.

  2. #2
    Senior Member
    Join Date
    Jun 2007
    Location
    Hyderabad, AP, India
    Posts
    806
    Thanks
    44
    Thanked 55 Times in 48 Posts
    Rep Power
    12

    Default

    Is this what your are looking at?
    Code:
    abcATabcDOTcom abccom
    pqrATabcDOTcom pqrabc
    godATabcDOTcom ghoabc
    --> to abc.com file..
    Code:
    appleATfruitsDOTcom applefruits
    orangeATfruitsDOTcom orangefruits
    guavaATfruistDOTcom guavafruits
    --> to fruts.com file..
    Code:
    laptopATcomputerDOTcom lapcomputer
    mouseATcomputerDOTcom moucomputer
    --> to computer.com file..

    Code:
    god1ATabDOTcom gh1oabc
    --> again to abc.com file..
    Code:
    hddATcomputerDOTcom hddcomputer
    pplATnixcraftDOTcom pplnixcraft
    adminATnixcraftDOTcom admnixcraft
    accountsATnixcraftDOTcom accountnixcraft
    and so on?
    Here is the code for above one. I am considering abc.txt as the file which contain above details.
    Code:
    #!/bin/bash
    sed -r 's/(.*)DOT(.*) /\1.\2 /g' abc.txt > abc1.txt
    for i in $(sed -r 's/(.*)AT(.*)DOT(.*) /\2.\3 /g' abc.txt | cut -d" " -f1 | sort | uniq)
    do
    grep "$i" abc1.txt > $i
    sed -ri 's/(.*)\.(.*)/\1DOT\2/g' $i
    done
    
    rm -rf abc*
    Last edited by nixcraft; 26th December 2012 at 11:55 PM.
    Thanks,
    Surendra Kumar Anne
    Linux: Fast, friendly, flexible and .... free!
    Support Open source.
    http://www.linuxnix.com

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Re: Search contents in a file and create files accordingly

    Dear Surendra,

    That was so nice and mine result required were something like that. All the left field are email addresses, nixcraft didnt allow me to put the email address field, so i just kept in that haphazard manner for email address filed. Can it be reviewed again considering the left part as the email address?

    Thank you so much for your prompt response.

    Regards,
    Franklin


    Quote Originally Posted by kumarat9pm View Post
    Is this what your are looking at?
    Code:
    abcATabcDOTcom abccom
    pqrATabcDOTcom pqrabc
    godATabcDOTcom ghoabc
    --> to abc.com file..
    Code:
    appleATfruitsDOTcom applefruits
    orangeATfruitsDOTcom orangefruits
    guavaATfruistDOTcom guavafruits
    --> to fruts.com file..
    Code:
    laptopATcomputerDOTcom lapcomputer
    mouseATcomputerDOTcom moucomputer
    --> to computer.com file..

    Code:
    god1ATabDOTcom gh1oabc
    --> again to abc.com file..
    Code:
    hddATcomputerDOTcom hddcomputer
    pplATnixcraftDOTcom pplnixcraft
    adminATnixcraftDOTcom admnixcraft
    accountsATnixcraftDOTcom accountnixcraft
    and so on?
    Here is the code for above one. I am considering abc.txt as the file which contain above details.
    Code:
    #!/bin/bash
    sed -r 's/(.*)DOT(.*) /\1.\2 /g' abc.txt > abc1.txt
    for i in $(sed -r 's/(.*)AT(.*)DOT(.*) /\2.\3 /g' abc.txt | cut -d" " -f1 | sort | uniq)
    do
    grep "$i" abc1.txt > $i
    sed -ri 's/(.*)\.(.*)/\1DOT\2/g' $i
    done
    
    rm -rf abc*

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 24th April 2012, 02:13 AM
  2. Replies: 2
    Last Post: 10th December 2011, 05:07 PM
  3. Replies: 1
    Last Post: 25th April 2011, 04:21 AM
  4. Script to combine contents of files
    By linuxgeek in forum Shell scripting
    Replies: 2
    Last Post: 14th April 2011, 04:57 PM
  5. contents of files
    By guest in forum Shell scripting
    Replies: 13
    Last Post: 13th September 2005, 07:41 AM

Tags for this Thread

Posting Permissions

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