Help with awk command
0
0
Entering edit mode
4.4 years ago
someler • 0

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 • 1.2k views
ADD COMMENT
2
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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!

ADD REPLY
0
Entering edit mode

Woops! Thank you, I will next time.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

100% positive it is only printing the last line.

ADD REPLY
0
Entering edit mode

what does:

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

give as output?

ADD REPLY
0
Entering edit mode

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
ADD REPLY
0
Entering edit mode

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?

ADD REPLY

Login before adding your answer.

Traffic: 2668 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6