Results 1 to 8 of 8

Thread: split files by specifying a string (bash shell)

Hybrid View

  1. #1
    Member
    Join Date
    Oct 2007
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Unhappy split files by specifying a string (bash shell)

    Hi all,

    I have a file of around 300 lines in which string "SERVER" occurs around 32 times.

    for eg.
    SERVER
    .....
    .....
    ....

    SERVER

    .....
    .....
    ....

    SERVER.....
    .....
    ....
    I need to split files like, for eg

    file1
    SERVER
    ....
    ....
    ....

    file2
    SERVER
    .....
    ...
    ....


    file3
    SERVER
    .....
    ....
    ....
    I am using this code
    awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final

    But the problem is that it makes maximum of 10 files, but I need more than 30.
    I have tried using nawk, but didnt worked. This cmd is running fine in Linux, But not in Sun OS (Solaris).
    I am using bash scripting on Sun OS.


    Any other way of splitting this data ???

    Pls help !!!

    Thanks in adv.
    Regards,
    Vikas
    Last edited by vikas027; 30th October 2007 at 12:58 AM.

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,515
    Thanks
    17
    Thanked 815 Times in 511 Posts
    Rep Power
    10

    Default

    I'm not sure if I understood your problem, can you explain it little bit..
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Member
    Join Date
    Oct 2007
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Quote Originally Posted by nixcraft View Post
    I'm not sure if I understood your problem, can you explain it little bit..

    Hi,

    Well I am explaining it again.
    I have a file say final.txt, which has some data in it.

    For e.g.
    final.txt

    SERVER
    w
    easf
    fwsfw
    fsdf
    23

    SERVER
    wefwrg
    234gf
    dfgh32
    fdfg3r
    gdfg34
    grer3467
    322rfgt45

    SERVER
    3211
    33
    453de
    43ffg
    Now, I need to split this file, by matching a string SERVER such than I have three files list1, list2, list3.

    As in this case, it may be:

    File list1
    SERVER
    w
    easf
    fwsfw
    fsdf
    23

    File list2
    SERVER
    wefwrg
    234gf
    dfgh32
    fdfg3r
    gdfg34
    grer3467
    322rfgt45

    File list3
    SERVER
    3211
    33
    453de
    43ffg
    Now, I know the command to split this file final.txt i.e.
    awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final

    But the problem I am facing is that my actual file final.txt is pretty big, which needs to splitted into around 30 files, which is however not being possible by this command, as it can only split upto 10 files at max only.

    Hope I have made my point clear.

    Thanks.

  4. #4
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,515
    Thanks
    17
    Thanked 815 Times in 511 Posts
    Rep Power
    10

    Default

    Try

    Code:
    awk '/SERVER/{n++}{print >"out" n ".txt" }' final.txt
    It will read input from final.txt and produced out1.txt, out2.txt... out30.txt
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  5. #5
    Member
    Join Date
    Oct 2007
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Smile Hi All

    Hi,

    I tried lots of commands including the above ones. They all run fine on Linux machines
    BUT not on the solaris machines, dont know the reason behind it.

    Anyways, MANY MANY THANKS to all for your time and help. I found this command to work perfectly.

    Code:
    /usr/xpg4/bin/awk '/SERVER/{n++}{print > f n}' f=/vikas/list /vikas/final
    Thanks again.

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default File Splitter 1.3

    hi,

    new features of File Splitter:

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    more helpful

  8. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default File Splitter 1.3

    Hi,

    File Splitter

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. shell script to open log files and check for faults
    By trueman82 in forum Shell scripting
    Replies: 3
    Last Post: 26th March 2013, 10:33 AM
  2. How can I set Path in bash shell
    By chanderbio in forum Shell scripting
    Replies: 10
    Last Post: 29th March 2012, 11:37 PM
  3. SCP with a bash shell password linux and unix servers
    By jerry in forum Networking, Firewalls and Security
    Replies: 1
    Last Post: 9th June 2007, 06:53 PM
  4. Replies: 2
    Last Post: 8th February 2007, 04:45 PM
  5. example for string connect to a command.
    By ryan in forum Shell scripting
    Replies: 2
    Last Post: 22nd February 2005, 02:05 PM

Posting Permissions

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