2

I put the output of find command into array like this:

pathList=($(find /foo/bar/ -type d))

How to extract the longest paths found in the array if the array contains several equal-length longest paths?:

echo ${pathList[@]}
/foo/bar/raw/
/foo/bar/raw/2020/
/foo/bar/raw/2020/02/
/foo/bar/logs/
/foo/bar/logs/2020/
/foo/bar/logs/2020/02/

After extraction, I would like to assign /foo/bar/raw/2020/02/ and /foo/bar/logs/2020/02/ to another array.

Thank you

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
kamokoba
  • 497
  • 9
  • 17
  • wrt `I put the output of find command into array like this:` - don't do that as it's not robust, e.g. it'll fail when your directory names contain spaces. See https://stackoverflow.com/q/23356779/1745001 for how to populate an array with the output of a command. Also, define "longest" - most total characters or must sub-directories or something else? – Ed Morton Feb 23 '20 at 00:22
  • Could you please confirm once on definition of longest path you want to get, I considered that you need output as per directory levels please confirm here. – RavinderSingh13 Feb 23 '20 at 02:35
  • 1
    @RavinderSingh13, yes by "longest path" I meant directory levels. Your solution works well, therefore I accepted it as the answer – kamokoba Feb 23 '20 at 06:02

1 Answers1

4

Could you please try following. This should print the longest array(could be multiple in numbers same maximum length ones), you could assign it to later an array to.

echo "${pathList[@]}" |
awk -F'/' '{max=max>NF?max:NF;a[NF]=(a[NF]?a[NF] ORS:"")$0} END{print a[max]}'


I just created a test array with values provided by you and tested it as follows:

arr1=($(printf '%s\n' "${pathList[@]}" |\
awk -F'/' '{max=max>NF?max:NF;a[NF]=(a[NF]?a[NF] ORS:"")$0} END{print a[max]}'))

When I see new array's contents they are as follows:

echo  "${arr1[@]}"
/foo/bar/raw/2020/02/
/foo/bar/logs/2020/02/

Explanation of awk code: Adding detailed explanation for awk code.

awk -F'/' '                        ##Starting awk program from here and setting field separator as / for all lines.
{
  max=max>NF?max:NF                ##Creating variable max and its checking condition if max is greater than NF then let it be same else  set its value to current NF value.
  a[NF]=(a[NF]?a[NF] ORS:"")$0     ##Creating an array a with index of value of NF and keep appending its value with new line to it.
}
END{                               ##Starting END section of this program.
  print a[max]                     ##Printing value of array a with index of variable max.
}'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    just a followup question - I tried to loop through arr1 and it appears that it contains only 1 item instead of two. How would you separate those two directory paths in arr1 so that they become two separate items? Thank you. – kamokoba Feb 23 '20 at 10:08
  • 2
    @kamokoba, could you please check my arr1 answer now and lemme know I am not on my desk as of now so didn't test should work but, lemme know – RavinderSingh13 Feb 23 '20 at 10:20
  • 1
    it works now after adding additional parentheses. Thank you, appreciate your time! – kamokoba Feb 23 '20 at 11:15