Bash loop to result in one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash loop to result in one file
# 1  
Old 09-16-2015
Bash loop to result in one file

Is there a way to use a directory with multiple files (.bam) to create one new file? The below bash loop will create a new file (header.sam) for each of the bam files. However, I only need to use 1 of the bam file to make 1 header file. I attempted a code as well, but not sure if thats right.Thank you Smilie.

So in this example there are 3 .bam files and the loop with create a new header file for each. However only one header file is needed.

Code:
123.bam
456.bam
789.bam

header.sam

Code:
     for f in /home/cmccabe/Desktop/NGS/pool_I_090215/*.bam ; do
     bname=`basename $f`
     pref=${bname%%.bam}
     samtools view -H $f > /home/cmccabe/Desktop/NGS/pool_I_090215/${pref}_header.sam
 done

Maybe:
Code:
     find f in /home/cmccabe/Desktop/NGS/pool_I_090215/*.bam ; do
     bname=`basename $f`
     samtools view -H $f > /home/cmccabe/Desktop/NGS/pool_I_090215/header.sam
 done


Last edited by cmccabe; 09-16-2015 at 03:45 PM.. Reason: fixed formatting
# 2  
Old 09-16-2015
Not sure I understand. If you want just one file - no matter which - why do run a loop and not one single command?
# 3  
Old 09-16-2015
A single command is what I am doing, but I am trying to make it so that the exact filename doesn't need to be used only the directory. Since the bam files change daily, it makes it a bit difficult to use. The directory is always the same. Thank you Smilie.

The individual command is:

Code:
samtools view -H IonXpress_009_150603.bam > header.sam

I am trying to do something like:

Code:
samtools view -H /home/cmccabe/Desktop/NGS/pool_I_090215/*.bam > header.sam

Thank you Smilie.

Last edited by cmccabe; 09-16-2015 at 04:08 PM.. Reason: added a couple lines
# 4  
Old 09-17-2015
So - how about samtools view -H $(ls /home/cmccabe/Desktop/NGS/pool_I_090215*.bam | head -1) > header.sam
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-17-2015
Code:
for f in /home/cmccabe/Desktop/NGS/pool_I_090215/*.bam ; do
     samtools view -H $f
done > entireloopfile

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 09-18-2015
Thank you Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies

2. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

3. Shell Programming and Scripting

Bash loop to search file

In the bash when the user inputs an id to search for the bash currently closes, and if a match is found outputs a new file (match.txt). Is it possible to have not close the bash but rather, on the screen "searching for match" and if a match is found "match found in line.." is displayed... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

5. Programming

Loop in bash file for mysql

Hi, I'm having problems with a script which some of you helped me with last week. It's a script to check the status of orders in an SQL database, and if the count(*) is zero, then the script stops. If it's non-zero, the script echos the result to a file and then in cron, I cat the file and mail... (3 Replies)
Discussion started by: davidm123SED
3 Replies

6. Shell Programming and Scripting

(BASH) Using a loop variable to grep something in a file?

Hi, I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i. I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep... (5 Replies)
Discussion started by: DMini
5 Replies

7. Shell Programming and Scripting

Find result using for loop

I want to print each file i found using the find command. And not able to list the files at all here is the code SEARCH_DIR="/filesinfolder"; PATH_COUNT=0 for result in "'/usr/bin/find $SEARCH_DIR -daystart \( \( -name 'KI*' -a -name '*.csv' \) -o -name '*_xyz_*' \) -mtime 1'" do... (1 Reply)
Discussion started by: nuthalapati
1 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

How to use while loop in bash shell to read a file with 4 lines of gap

Hi , I am currently using the while loop in bash shell, as follows. while read line do echo $line done < file.txt However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap. Ex- if file.txt is a file of 100 lines, then i want to use the loop such... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

10. Shell Programming and Scripting

Outputting formatted Result log file from old 30000 lines result log<help required>

Well I have a 3000 lines result log file that contains all the machine data when it does the testing... It has 3 different section that i am intrsted in 1) starting with "20071126 11:11:11 Machine Header 1" 1000 lines... "End machine header 1" 2) starting with "20071126 12:12:12 Machine... (5 Replies)
Discussion started by: vikas.iet
5 Replies
Login or Register to Ask a Question