2

How can I convert this:

-foo -bar 42 \
     randomtext=00 "00" \
          -randomtext=00 "00" \
-foo -bar 104 \
     randomtext=00 "00" \
-foo -bar 1 \
          -randomtext=00 "00" \

To this:

-foo -bar 1 \
          -randomtext=00 "00" \
-foo -bar 42 \
     randomtext=00 "00" \
          -randomtext=00 "00" \
-foo -bar 104 \
     randomtext=00 "00" \

I would like to sort the clusters of lines based on the numeric value after -bar

6
  • cut and sort are your friends...
    – Fabby
    May 6, 2015 at 19:25
  • Most definitely. I am just uncertain on how to get the entire lines + the extra leading lines
    – TuxForLife
    May 6, 2015 at 19:26
  • 1
    Any way to simplify this? Like, can we always assume that the first entry of each group starts with -and that the inner entries always don't?
    – kos
    May 6, 2015 at 19:38
  • GREAT question kos, unfortunately, some following lines will have a "-". I edited my question like so. However, the first entry of each group will start with "-foo"
    – TuxForLife
    May 6, 2015 at 19:41
  • Last question, is indentation done with tabs? Spaces? In any case, any other sequence of tabs / spaces aside from those used for indentation?
    – kos
    May 6, 2015 at 19:54

2 Answers 2

1

Using python:

#!/usr/bin/env python2
import re, sys
list_of_lines = []
with open(sys.argv[1]) as f:
    for line in f.read().split('-foo'):
        if line:
            list_of_lines.append(line)
    for line in sorted(list_of_lines, key=lambda i: int(re.search(r'(?<=-bar )\d+', i).group())):
        print '-foo' + line.rstrip()

Output :

-foo -bar 1 \
          -randomtext=00 "00" \
-foo -bar 42 \
     randomtext=00 "00" \
          -randomtext=00 "00" \
-foo -bar 104 \
     randomtext=00 "00" \

How to run :

Save the file as e.g. script.py, make it executable and then pass the file you want the script to opertate on as the first argument:

/path/to/script.py /path/to/file.txt

If the script and file are on the same directory, then from that directory:

./script.py file.txt

You can run it without making executable by passing the script as an argument to python executable:

python2 script.py file.txt
5
  • 2
    Hey heemayl, thanks for an answer once again, what would I have to change to make it work like so: /heemayls/script file
    – TuxForLife
    May 6, 2015 at 20:08
  • 2
    Cool heemayl as a script folder on your system 😁
    – A.B.
    May 6, 2015 at 20:19
  • @TuxForLife: I am flattered :D ..check my edits..
    – heemayl
    May 6, 2015 at 20:47
  • Absolutely outstanding. I really need to learn python, I have tried, but I get lost after a while. You have inspired me to try harder, thank you.
    – TuxForLife
    May 6, 2015 at 20:55
  • 1
    @TuxForLife: Thats a very big statement, i am no one really to be inspired from :) ..there are many sound people around here, you should take inspiration from them :)....and yeah, python is fun..once you get a grip of it many tasks would be somewhat simpler for you..start learning again :)
    – heemayl
    May 6, 2015 at 21:02
1

The perl way:

#!/usr/bin/perl
$filename=$ARGV[0];
open(my $fh, "<", $filename) or die "cannot open < $filename: $!";

my %hash, my $key;
while (my $row = <$fh>) {
  chomp $row;
  if ($row =~ /\-bar\s+([0-9]+)/ ) {
    $key = $1;
  }
  $hash{$key} .= "$row\n";
}

foreach (sort { $a <=> $b } keys(%hash) ) {print "$hash{$_}"}

Save the script and call the executable (chmod +x script) with:

script file.txt

Output:

-foo -bar 1 \
          -randomtext=00 "00" \
-foo -bar 42 \
     randomtext=00 "00" \
          -randomtext=00 "00" \
-foo -bar 104 \
     randomtext=00 "00" \
1
  • 1
    Worked like a charm, it's great to have a surplus of working methods!
    – TuxForLife
    May 7, 2015 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .