I have a string of pre defined ip address list which will always remain constant their order will never change like in below sample:
iplist=8.8.5.19,9.7.5.14,12.9.9.23,8.8.8.14,144.1.113
In the above example i m considering only 5 ips but there could be many more.
Now i have a file which will have a subset of these IPs along with a few file details and few blank empty new lines in any random order in each run like in the sample data below.
cat hello.txt
144.1.113
-rwxrw-rw- /tmp/var/file11.txt 56427
8.8.5.19
-rwxrw-rw- /tmp/var/file1.txt 56427
-rwxr--rw- /tmp/var/file2.txt 44427
-rwxr--rw- /tmp/var/file5.txt 36427
8.8.8.14
-rwxrw-rw- /tmp/var/file21.txt 56427
-rwxr--rw- /tmp/var/file22.txt 44427
I wish to format sort the above output in same order as the iplist variable. My expected output is as below:
8.8.5.19
-rwxrw-rw- /tmp/var/file1.txt 56427
-rwxr--rw- /tmp/var/file2.txt 44427
-rwxr--rw- /tmp/var/file5.txt 36427
9.7.5.14
12.9.9.23
8.8.8.14
-rwxrw-rw- /tmp/var/file21.txt 56427
-rwxr--rw- /tmp/var/file22.txt 44427
144.1.113
-rwxrw-rw- /tmp/var/file11.txt 56427
My idea is to have two have to arrays one that stores the iplist in the same order as iplist variable and the second that stores its respective file details (blank incase that ip is not present in my output). That way i will be able to use a single counter and display the desired output in the desired order.
Considering my approach is fine I was able to add the iplist string to an array using the below:
filedets=()
IFS=',' read -ra my_array <<< "$iplist"
for i in "${my_array[@]}"
do
if [ grep $i hello.txt ]; then
# need logic to get file details for the matched IP
filedets+=
else
filedets+=""
fi
done
I'm able to loop this array and store empty entries incase the ip is not found the hello.txt
The challenge for me is how can I extract the file details under a particular ip and store in a parellel array.
Any good way to have such a custom grouping and sorting? Kindly suggest.