AnnaSchumann
AnnaSchumann

Reputation: 1271

Printing lines if a specific column contains values >0

I have a tab-delimited .txt file in this format, containing numerous symbols, numerals and letters:

MUT 124 GET 288478  0   *       =   288478  0
MUT 15  GET 514675  0   75MH    =   514637  -113
MUT 124 GET 514637  0   75MH    =   514675  113

I want to identify all lines that contain a >0 value in the 9th column (i.e. only the 3rd row above would be extracted) and then print column 4 + 9 from any matched lines.

Desired output (two column tab delimited .txt file):

514637    113

Is there a quick way to do this in terminal/on the command-line. If so, how?

I've only just begun to learn awk and perl so all my attempts so far have been nowhere near close. Not sure where to begin!

Upvotes: 0

Views: 1077

Answers (3)

jreisinger
jreisinger

Reputation: 1643

Can be done with the Perl one-liner:

$ perl -anE 'say join "\t", @F[3,8] if $F[8] > 0' data.txt
  • -n (non-autoprinting) - loop through lines, reading but not printing them
  • -a (auto-split) - split the input line stored in $_ into @F array (space is the default separator, change it with -F, ex. -F:)
  • -E 'CODE' (execute) - execute 'CODE' enabling feature bundle (like use 5.010) for your version of Perl

See perlrun for more.

Upvotes: 3

fedorqui
fedorqui

Reputation: 290155

awk handles it almost automatically!

awk '$9>0 {print $4,$9}' file

If you need to specify the input and output separator, say:

awk 'BEGIN{FS=OFS="\t"} $9>0 {print $4,$9}' file

Upvotes: 1

choroba
choroba

Reputation: 242038

Easy in Perl

perl -lane 'print "$F[3]\t$F[8]" if $F[8] > 0' < input-file
  • -l appends a newline to everything you print
  • -a splits the input into the @F array
  • -n processes the input line by line

Upvotes: 4

Related Questions