i have sh program which search for a file in a folder structure and provides its path. This is just used to see if that file exits more that once anywhere down the folder structure. I have used find command to search & printing it output on terminal.
I have attached screen shot of it. only help i need is to make is better aligned which i am not able to do it.
clear
search_folder=/home/pks/Desktop # Folder in which i am searching
while read line;
do
echo File name is:-$line -- `find $search_folder -name $line` # finding the file name from office.txt in search folder.
done < /home/pks/Desktop/office.txt # File from which i am reading
What would you call "better aligned"? An output sample would seriously help here.
And, wouldn't it be better to traverse the directory structure just once in lieu of doing it for every line in office.txt, running the risk of find ing more than one file? Think about find ing all files under the search_folder, piping them to an awk script that filters and formats?
Sorry..it just been few days(max 5 days) working with shell. Dont know awk but will try hand on that for sure.
Better aligned means better or proper spacing to make them look better for reading and understnding :-
I am just writing space to make my point because when i am submitting tab is getting auto removed in the forum.
File name is:- Hello.txt{SPACE}--/home/pks/Destop/sumit/Dirty/empty_dir/empty.file{Space}/home/pks/Desktop/sumit/empty.file
File name is:- HelloWorld.py{ }--/home/pks/Destop/sumit/Dirty/HelloWorld.py{Space Space }/home/pks/Desktop/sumit/HelloWorld.py
I have checked with printf and tried everything but still the output is not as i am expecting but it 90% correct
in attached screen shot if you will see it is adding tab before printing both the output but not in next line. it is showing two type of put put line 1 & 4 is one way rest all line is another way. there is something wrong with TAB but not able to find what.
while read file
do
printf "File:-%s" "$file"
find "$search_folder" -name "$file" -type f |
while read path
do
printf "\t%s" "$path"
done
echo
done < /home/pks/Desktop/office.txt
---
Or with awk:
find "$search_folder" -type f |
awk '
NR==FNR {
A[$0]
next
}
$NF in A {
A[$NF]=A[$NF] OFS $0
}
END {
for(i in A) printf "File:-%s%s\n",i,A
}
' FS=/ OFS='\t' /home/pks/Desktop/office.txt -
---------- Post updated at 03:21 PM ---------- Previous update was at 03:19 PM ----------
Hi All,
i think i ll have to live with this out put until i find some alternative or other way. I ll keep trying & i ll share it with you guys when it will work as the way i want.
Thanks everyone for all your support.
Note:- i am closing this thread as solved as i got enough to carry on my learning & fun work
Try formatted output, adapt field lengths if need be:
while read file
do
printf "File: - %-15s --" "$file"
find "$search_folder" -name "$file" -type f |
while read path
do
printf " %50s" "$path"
done
echo
done < /home/pks/Desktop/office.txt
or, adapting Scrutinizer's awk approach, try
find "$search_folder" -type f | awk '
NR==FNR {A[$0]
next
}
$NF in A {A[$NF]=sprintf ("%s\t%50s", A[$NF], $0)
}
END {for(i in A) printf "File:-%-15s -- %s\n",i,A
}
' FS=/ /home/pks/Desktop/office.txt -