LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH loop to write out file contents with a portion of the file path (https://www.linuxquestions.org/questions/linux-newbie-8/bash-loop-to-write-out-file-contents-with-a-portion-of-the-file-path-4175520875/)

psanjuan 10-02-2014 03:58 PM

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.

Thanks for any help!

unSpawn 10-02-2014 05:50 PM

Something like:
Code:

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

?

grail 10-02-2014 09:23 PM

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

Hope some of that helps.

unSpawn 10-03-2014 12:43 AM

Quote:

Originally Posted by grail (Post 5248304)
1. No closing back quote to match the one before ls

...and using 'ls' instead of 'find': http://mywiki.wooledge.org/ParsingLs

grail 10-03-2014 01:27 AM

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 ;)


All times are GMT -5. The time now is 01:15 PM.