This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help with awk command

I have a file with 50 lines of sample names. I have use this awk one liner before, but today, it only prints out the 50th line of the file. I expect this command to print all sample names ("Sample1", "Sample2", "Sample3", "Sample4"), but it doesn't. Can anyone help me figure out why? File example and command below.

Command:

SHORT=`echo $FILE | awk -F "_" '{print $1}'`

Sample1_F.txt
Sample2_R.txt
Sample3_F.txt
Sample4_R.txt

When I print this command to a file I only get "Sample4".

awk print

this can hardly be the complete command you try to execute. Can you also post the remainder? (eg. what is $FILE ?)

It's not. I was trying to not have to make my entire script anonymous. This is the beginning of my script to add read groups:

FILELIST=`cat Samples.txt`

for FILE in $FILELIST

do

SHORT=`echo $FILE | awk -F "_" '{print $1}'`

java -jar $picard_path I=$FILE O="$SHORT".rg.bam RGID=$SHORT RGLB=library RGPL=platform RGPU=cohortname RGSM=$SHORT
done

It should be running this for all 50 samples, but only does it for the last one.

Please use the formatting bar (especially the code option) to present your post better. I've done it for you this time.
code_formatting

Thank you!

Woops! Thank you, I will next time.

Are you sure its the last line and not the first one? Maybe the newline is wrong. Think about what the awk actually does. (edit misread statement).

100% positive it is only printing the last line.

what does:

for file in `cat Samples.txt`; do echo $file; done

give as output?

This echoes all my lines. This surprised me because when I do this command it prints the last line. When I run my fill script, it returns a bam file with read groups to the last sample from that file. Confused about where the disconnect is.

for file in `cat Samples.txt`; do echo $file; done > TestFile.txt

that is strange indeed. With me it does print all entries to the output file. Your output looks more like what you would get with this command:

for file in `cat Samples.txt`; do echo $file > TestFile.txt ; done

If your script is a loop in loop kinda system, can you check you did not put the done in the wrong place?

which shell (version?) are you using btw?

0 answers

No answers yet.

Log in to answer this question.