[SOLVED] BASH loop to write out file contents with a portion of the file path
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH loop to write out file contents with a portion of the file path
I am trying to write a BASH shell script to write out the contents of multiple similar files together with a portion of the file path (which is an ID number).
I have been trying variations on:
for i in `ls /export/research/analysis/human/psanjuan/combat_08247/AUTO_ANALYSIS/triotim/M*/S*/AOD_V01_R0*/behavioral/CALC_RT/hit_rt_run* |cat ; echo "$i"| cut -d "/" -f10,15 ; done
I think I'm close, but it hangs up when I try this.
What I want is to have displayed the contents of each file (a list of reaction times) with the M* value (the ID number of each person) attached to each one. In a perfect world, this would be in columns, but I can live with that not being the case.
BASE_PATH="/export/research/analysis/human/psanjuan/combat_08247/AUTO_ANALYSIS/triotim"
find ${BASE_PATH}/* -maxdepth 0 -type d -name M\* -printf "%f\n"| while read PERSON_ID; do
find ${BASE_PATH}/${PERSON_ID} -type f -iname \*hit_rt_run\* | while read PERSON_FILE; do
cat ${PERSON_FILE} | while read LINE; do echo "${PERSON_ID}: ${LINE}"; done
done|column -t
done
Whilst unSpawn's solution will work, you need to be cautious as the final find is searching for 'hit_rt_run*' but under all directories under 'M*', so if there other alternatives to 'S*/AOD_V01_R0*'
you will get more data back than you bargained for.
On the code you presented, there are a few issues:
1. No closing back quote to match the one before ls
2. No 'do' to advise the for loop on when to start processing
3. The cat serves no purpose as it is getting its information from stdin (due to the pipe) hence it will just repeat what was ls'ed to it and not cat the contents of each file
Unfortunately I have given up on trying to quote that one (as much as i believe in it) as those that get caught by the issues with it learn quickly and those who don't, won't ... at least for now
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.