LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-02-2014, 04:58 PM   #1
psanjuan
LQ Newbie
 
Registered: Oct 2014
Posts: 1

Rep: Reputation: Disabled
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!
 
Old 10-02-2014, 06:50 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
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
?
 
Old 10-02-2014, 10:23 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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.
 
Old 10-03-2014, 01:43 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
Quote:
Originally Posted by grail View Post
1. No closing back quote to match the one before ls
...and using 'ls' instead of 'find': http://mywiki.wooledge.org/ParsingLs
 
Old 10-03-2014, 02:27 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Move file contents to another file with bash script (find, mimetype and cat) _furrycat_ Linux - Newbie 1 10-06-2013 12:12 PM
[SOLVED] Bash, when I read sort write md5 file, the writen file have a space missing peter1234 Linux - General 3 09-21-2010 10:04 AM
Bash remove part of a file based on contents of another file bhepdogg Programming 4 01-31-2007 04:13 PM
Only load last portion of a file, instead of the whole really big text file 1veedo Linux - General 2 02-20-2006 07:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration