0

I am using the Ubuntu bash. My file contains paths from a filesystem.

E.g.

C:\users\username\subfolder\thisismyfile.txt
C:\users\username\subfolder\thisismyfileplusadate.txt
C:\users\username\subfolder2\thisismyfileplusname.txt
C:\users\username\subfolder3\thisismyfile.txt

What I want to do:

Compare each start of each line with the start of the next line.

E.g.

First n characters of line 1 first n characters of the following line. If this substring is identical I want to compare the complete length of the two lines and the longer path of the two should be printed to an output file concatenated with the length of that longer path.

So for my example above:

C:\users\username\subfolder\thisismyfile.txt
C:\users\username\subfolder\thisismyfileplusadate.txt

The first n characters, let's say 28 so "C:\users\username\subfolder\" would be compared. They are identical. Next I want to compare the length of the lines to each other. Line 1 would be 44 characters and line 2 53.

So I want to print line 1 to an output file and concatenate "length: 53" to the string (or the beginning of the line)

How would I go about this?

  • Do you need to compare pairs of lines (1 against 2, 3 against 4 and so on) or every line against the next (1 against 2, 2 against 3, ...)? – steeldriver Jun 17 at 12:29
  • Thank you for your question. Every line against the next. So n to n+1, n+1 to n+2, n2 to n+3 and so on. – user358244 Jun 17 at 12:47
  • Case sensitive? – Jacob Vlijm Jun 18 at 5:40
  • Not really. All the paths are similar enough so lower-/uppercase is not relevant – user358244 Jun 18 at 8:22
0
#!/bin/bash 

file_len=$(wc -l afile.txt | awk {'print $1'});
let "flen = $file_len-1";
nth=28;
for i in $(seq 1 $flen); do {
        li_cut="";
        ly_cut="";
        let "y = $i +1";
        line_i=$(sed -n "$i"p afile.txt);
        len_i=$(echo $line_i | wc -m)
        li_cut="${line_i:0:$nth}";
        line_y=$(sed -n "$y"p afile.txt);
        len_y=$(echo $line_y | wc -m)
        ly_cut="${line_y:0:$nth}";

        if [ "$li_cut" == "$ly_cut" ]; then {
                if [ $len_i -gt  $len_y ]; then {
                        echo "$line_i [ $len_i ];"
                } fi
                if [ $len_y -ge $len_i ]; then  {
                        echo "$line_y [ $len_y ]";
                } fi;
        } fi;
} done

something like this ? Won't be that good with huge txts and might be probably coded in some prettier forms.

Edit: it might be useful to use a sort|uniq in the end, given that: if line n+1 > n and line n+1 > n+2 you'll get a 'repeated' n+1

0

To print the content of the longer path alongside the length of that path:

awk -v n=28 '
  substr($0,1,n) == substr(last,1,n) {
    if(length($0) > length(last))
      printf("length: %d %s\n", length($0), $0);
    else
      printf("length: %d %s\n", length(last), last);
  }
  {last = $0}
' file
length: 53 C:\users\username\subfolder\thisismyfileplusadate.txt
  • Thank you for your reply! I edited my original post. I want to print the content of the longer path alongside the length of that path. – user358244 Jun 18 at 8:25
  • @user358244 please see updated answer – steeldriver Jun 18 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.