Bash for loop pull text from file recursively -
i having trouble writing bash loop script can extract contents of specific file common many child directories under parent directory.
directory structure:
/parent/child/grand_child/great_grand_child/file
in there many child, grandchild, , great grandchild folders.
i want script (in english):
for every grand_child folder, in every child folder:
- search through 1 great_grand_child folder
- find file named 0001.txt
- print text in row 10 of 0001.txt output file
- in next column of output file, print full directory path file text extracted from.
my script far:
for in /parent/**; if [ -d "$i" ]; echo "$i" fi done
can have designing script? far gives me path each grand_child folder, don't know how isolate 1 great_grand_child folder, , ask text in row 10 of 0001.txt file inside great_grand_child folder.
thanks in advance guidance or help.
# every grandchild directory parent/child/grandchild grandchild in parent/*/* # file $grandchild/greatgrandchild/0001.txt file in "$grandchild/"*/0001.txt # if there no such file, skip grandchild directory. if [ ! -f "$file" ] echo "skipping $grandchild, no 0001.txt files" >&2 continue fi # otherwise print 10th line , file came from. awk 'fnr == 10 { print $0, filename }' "$file" # don't @ more 0001.txt files in grandchild directory, # care 1 of them. break done done
Comments
Post a Comment