Results 1 to 7 of 7

Thread: Conditionally removing lines from a text file.

  1. #1
    Join Date
    Jan 2014
    Location
    UK
    Beans
    78
    Distro
    Xubuntu 16.04 Xenial Xerus

    Red face Conditionally removing lines from a text file.

    Hi
    I have text files, example attached below.
    They are lists of generated Sudoku puzzles.
    The number in the brackets is the difficulty rating.
    How could I process the files to delete the puzzles that are too easy or too difficult?
    For example, those less than 3000 or more than 4000.
    I suppose that grep would find the brackets, but I don't know what to do next.
    Attached Files Attached Files

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Conditionally removing lines from a text file.

    Something like

    Code:
    awk 'substr($NF,2,length($NF)-2) < 3000' test.txt
    or
    Code:
    perl -anle '@F[-1] =~ s/[()]//g ; print if @F[-1] > 4000' test.txt
    maybe? or since the (...) are valid numeric expressions, perhaps even

    Code:
    perl -anle 'print if eval @F[-1] > 4000' test.txt

  3. #3
    Join Date
    Jan 2014
    Location
    UK
    Beans
    78
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Conditionally removing lines from a text file.

    Quote Originally Posted by steeldriver View Post
    Something like...

    OK, thanks for that.
    I can see that the first command displays in console the ones that are too easy.
    And the other two commands display in console the ones that are too difficult.

    Now I need to work on a Goldilocks command...
    (Where the porridge is not too cold and not too hot)

    @xubuntu:~$ awk 'substr($NF,2,length($NF)-2) < 3000' test.txt
    .2............86.14.813....3...8....6.9.5...37.... 9......4278.........5.2.1...4.. #Extreme #1 Extreme (2458)
    ....4..8.6...........5962.48.........9.612........ .7....19....8.854..9....47.1..6 #Extreme #3 Extreme (2664)
    ....5.....87...3..1.5.7.984...284...5..19.6....... ...............6..7.389.4....1. #Extreme #4 Extreme (2338)
    .2...6.87......1...43.75....7...94....8.6......... ...5...8....4.324...6.7.....2.9 #Extreme #6 Extreme (1958)
    5...1.7...14.....6.2..3.9....5....74.7...9.3...2.6 .......9.1...7..6..29.....75.8. #Extreme #8 Extreme (2480)
    5....3....37.4.816.9..............68.89..61.7...4. .....489.....96..1..5...37..... #Extreme #9 Extreme (2920)
    .............716....3..651...1.543....5..7..24.81. ........9847.5.2.43............ #Extreme #11 Extreme (2974)
    29............4...57.28.......145..3....63..96.... .45....37.9..9.....71.....5.6.. #Extreme #12 Extreme (2374)
    5..............6.8..6345.....7..2....2.56.1..935.. ....1.....8.2....59.....4.1...6 #Extreme #13 Extreme (2108)
    3.42.759..7.......5...4.....6..73..18.542.37...... ...........2.....8...37..3.91.. #Extreme #14 Extreme (1920)
    .4..3...55..8..4.16.8...2...2.5..71.9...........2. 6..........9...1.48.......5762. #Extreme #16 Extreme (2194)
    3.5............243..1..2.9....6....76.8..........8 13..5.........825....9.4.2637.. #Extreme #17 Extreme (2924)
    .8395.7..95...7........8....2.5.........19..23...2 ..5.1.....38.7...4...9.4..8.1.. #Extreme #18 Extreme (1830)
    .4...69..6...43....73.214..43.....2............2.3 .1..35.8.4..18.1..............7 #Extreme #19 Extreme (2774)
    1.562........8....63.......89....4...43956.....1.. ..7......81.6.....782......3.4. #Extreme #20 Extreme (2934)
    .....73.........294...5...1.49..1.7.1..9.......6.. 8....6.2...95.3.1..8.........4. #Extreme #21 Extreme (2142)
    9.54..2.6.....18..1.4...9.....6...4.........9....4 73815.61.9...87..............62 #Extreme #22 Extreme (2020)
    1....8.6.4361.5..2....2...........37527.....1..3.. .95....6......4.......6..783... #Extreme #23 Extreme (2084)
    ......9834....1....58.........68..........1.7..... 5.427.4.9......5..8.......136.. #Extreme #25 Extreme (1876)
    ...9....2..7..241...9.3.6....4.......2.64.1...5... ....43..71..59...5..34..2....6. #Extreme #26 Extreme (2310)
    ......2.5..1.4.8633.............3.....89..7..6..45 ....5.38...1...421.....8.6..... #Extreme #27 Extreme (2328)
    6..1....4..........4..8.237....438.69............. 79.5...7....3..4......53.42..6. #Extreme #28 Extreme (2778)
    ...64.853...........3..8..62......3.....15.975..9. 7...........47...5.3..896..47.. #Extreme #34 Extreme (2060)
    4658..........3.4.7....5....2......7....6...8....7 .492..........59...3..341....25 #Extreme #35 Extreme (2408)
    ..............45.75..3..1427.693.8...94.1...5..1.. ..6..7.1.....3..58........6.9.. #Extreme #38 Extreme (2700)
    1............5..36..37..59.845.91....1.........98. ......2...4.7.3..4....56....9.. #Extreme #39 Extreme (2638)
    .2.7..........61....93..84...1...9.....4.....6...3 ..21.8......43.592..........5.8 #Extreme #41 Extreme (2292)

    @xubuntu:~$ perl -anle '@F[-1] =~ s/[()]//g ; print if @F[-1] > 4000' test.txt
    ..47.82.65.7...41.8.......723.6.19.....42.18...... .....2.3.....1....7...6.31.2... #Extreme #2 Extreme (6960)
    64............2.5..59...17..3.5..8.....4...39..6.2 ...51..9......8..1.6..5....83.. #Extreme #5 Extreme (4634)
    1.4..7.......3......2...34.............39..5..63.1 42..2.....76..9.........56..43. #Extreme #24 Extreme (7058)
    ...2..4....7..........34.....1.67..5.98..17....... ..9...6...9...5.4.28.191..8...3 #Extreme #31 Extreme (4154)
    93.7.58......2...7..1.6..........6..1.98.......7.. 1.......4.1.2...257...5......7. #Extreme #32 Extreme (4012)
    ....8......476..2..8.3125............428.....6.5.. .2..7.823.49........5..39..687. #Extreme #40 Extreme (4418)
    ..6.5.4...182..........3.....91.....24......1..... .56.4..3.9..2....6.8.71...7..9. #Extreme #42 Extreme (6458)
    4.....7...89....2.........635..1.......43.9....... 6..2.1.28..4..4.....5.8.6..3..1 #Extreme #43 Extreme (4394)

    @xubuntu:~$ perl -anle 'print if eval @F[-1] > 4000' test.txt
    ..47.82.65.7...41.8.......723.6.19.....42.18...... .....2.3.....1....7...6.31.2... #Extreme #2 Extreme (6960)
    64............2.5..59...17..3.5..8.....4...39..6.2 ...51..9......8..1.6..5....83.. #Extreme #5 Extreme (4634)
    1.4..7.......3......2...34.............39..5..63.1 42..2.....76..9.........56..43. #Extreme #24 Extreme (7058)
    ...2..4....7..........34.....1.67..5.98..17....... ..9...6...9...5.4.28.191..8...3 #Extreme #31 Extreme (4154)
    93.7.58......2...7..1.6..........6..1.98.......7.. 1.......4.1.2...257...5......7. #Extreme #32 Extreme (4012)
    ....8......476..2..8.3125............428.....6.5.. .2..7.823.49........5..39..687. #Extreme #40 Extreme (4418)
    ..6.5.4...182..........3.....91.....24......1..... .56.4..3.9..2....6.8.71...7..9. #Extreme #42 Extreme (6458)
    4.....7...89....2.........635..1.......43.9....... 6..2.1.28..4..4.....5.8.6..3..1 #Extreme #43 Extreme (4394)

  4. #4
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Conditionally removing lines from a text file.

    Code:
    $ perl -anle 'print if eval @F[-1] > 3000 && eval @F[-1] < 4000' test.txt
    .4..9..1..5.7..........39....5.4.8..28...1............3.785..4...42....3.2....7.8 #Extreme #7 Extreme (3808)
    ....6..59...7.8..........1...9..2...6......7...5189.4..8........612..5.......479. #Extreme #10 Extreme (3662)
    8.2............1..94....8..3..87...6.1.9.3.....72.....45..6..71.............9825. #Extreme #15 Extreme (3358)
    .....96...194.....5.3.6.......1287..73.....8......7..6............68492.85.....7. #Extreme #29 Extreme (3574)
    .........2...14.5...7...2.459............8..1...935..61...4.6...2.....858...26... #Extreme #30 Extreme (3568)
    .85..124.16.......9..8......4...7.5..592....7....3...6..6.........1.5.34....235.. #Extreme #33 Extreme (3170)
    ..58..4..9.8....7.......3.57.63.5.4....97..3.2.........9.....24...58.......7.91.. #Extreme #36 Extreme (3624)
    ....1.3..1..47...........27...8...455....6....32.45.61..3....8...59.....7...5.... #Extreme #37 Extreme (3018)

  5. #5
    Join Date
    Jan 2014
    Location
    UK
    Beans
    78
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Conditionally removing lines from a text file.

    Thanks steeldriver, that's done the job.
    Code:
    (perl -anle 'print if eval @F[-1] > 3000 && eval @F[-1] < 4000' test.txt) > output.txt
    Attached Files Attached Files

  6. #6
    Join Date
    Mar 2015
    Location
    europe
    Beans
    Hidden!
    Distro
    Ubuntu Gnome

    Re: Conditionally removing lines from a text file.

    just for fun:
    Code:
    awk -F '[)(]' '$2>2500 && $2<3500' test.txt

  7. #7
    Join Date
    Jan 2014
    Location
    UK
    Beans
    78
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Conditionally removing lines from a text file.

    Quote Originally Posted by bardo2 View Post
    just for fun...
    Yes, this does the job also.
    Code:
    (awk -F '[)(]' '$2>3000 && $2<4000' test.txt) > output.txt

Bookmarks

Posting Permissions

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