Announcement

Collapse
No announcement yet.

string operations

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    string operations

    Dear all
    I have three problem please solve


    First Problem
    suppose I have one file like this

    abcd 124 12 1224 1222
    def3324
    aaaa
    ddddd33 333
    2
    22222
    wwwwwwww
    ccccccc
    dddddd
    ffffff
    jjjjj
    235222


    in this file every line is unique. and there are thousand no of lines now I want to view this file starting from text "aaaa" to "fffff" how can i do this.



    Second Problem is

    one of my script is recording logs to a file every Minuit. now the log file has become too large

    Now I want that my script shoud stop recording logs when it completed 10000 no of lines to that perticular file and should use another file instead of the first
    please suggess..

    third problem

    one of my script is creating file regularty in a director by name filename.$i where $i is incremental up to unilimited now i want that my directory
    should have only ten files if eleventh file is created by script then the first one (oldest onle ) file should be deleted automatically

    please suggess


    thaks

    #2
    Possible solutions:

    #1 print lines beginning with aaaa through ffff
    echo '/^aaaa$/,/^ffffff$/p' | ed -s file
    Using the ed editor as a "stream editor" this prints the lines starting with aaaa through the line ffffff

    #3 files created with xxx.N where N increments by one and only the latest 10 files retained:
    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13
    do
    touch xxx.$i # creates a file named xxx.N
    [ -f xxx.$((i-10)) ] && rm xxx.$((i-10)) # remove file N-10 if it exists
    done

    The line with "touch" creates a file presumably the file gets created in your script some other way. The next line does calculations based on an integer in the $i variable. If a file named xxx.M exists, where M is $i - 10, the file is removed.

    Hope this gives you some ideas.

    Comment


      #3
      #1 This is an awk or perl one liner.
      Code:
      awk '/aaaa/,/fffff/' [i]file[/i]
      perl -e 'while(<>) {print if /aaa/../bbb/}' [i]file[/i]
      #2 If your script is already running, and you can't stop it, and it keeps the log file open, I don't know how to do that. However, a lot of scripts don't keep the file open; they implicitly close it on every write. F.ex. bash scripts might echo "log message" >> $log. If that's the case, you can just rename the log file while it's running. You can tell by renaming the file; if the output just keeps going to the file even though it's name has changed, then the script keeps it open.
      If you can set things up before the script runs, just pipe it's output to the split command, say
      Code:
      [i]your_script[/i] | split --numeric --suffix 4 --lines 10000 - log
      If you don't have control over where the script writes it's log, you could fool it by using a fifo; say the log file is script_log:
      Code:
      mkfifo script_log
      [i]your_script[/i] &
      split --numeric --suffix 4 --lines 10000 script_log log
      #3 If you can change the script, and $i is the variable that has the number of the output file, whenever the script creates a new file just add (assuming bash):
      Code:
      rm -f filename.$((i - 10))
      If you can't modify the script, inotify-tools has stuff you could use to have a script watch a directory and delete old files.

      Regards, John Little
      Regards, John Little

      Comment

      Working...
      X