LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2016, 01:31 PM   #16
nash-c
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled

EUREKA!!

Ok just to clarify, I apologise grail for not copying and pasting your last command. I just tried this:

Code:
awk '$10 < -1000 && $10 > -3000{print > "filt_" FILENAME}' Int_DU145withTannopeaks.txt
and still got the following error:

Code:
awk: syntax error at source line 1
 context is
	$10 < -1000 && $10 > -3000{print > "filt_" >>>  FILENAME <<< }
awk: illegal statement at source line 1
I don't understand though why it works for you grail and not me. I'm probably doing something wrong still.

It seems that your suggestion ntubski works. When I tried using the command with the parentheses added, it worked fine for one single file. I then took it further and tried it on multiple files in my directory and this also worked great.

So for future readers, to clarify, you can filter a text file for a range of negative numbers in a specific column (column 10 in this case) on multiple files with this without for loops etc:

Code:
awk '$10 < -1000 && $10 > -3000{print > ("filt_" FILENAME)}' *.txt
or I still maintain that the original edit with the for loop does actually work too but perhaps with caveats:

Code:
for foo in $(ls -1 *.txt); do cat $foo | awk '{ if ($10 < -1000 && $10 >= -3000) print $0;}' >> filtered_$foo ; done
 
Old 03-23-2016, 01:47 PM   #17
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
@schneidz - it seems Apple use the idea that a rock steady base should never need upgrading ... cause who would ever use the command line on an Apple All of the base utils (find, tar,...) are all ancient and it is a real pain when my Apple family members say "Can't you just do it like on your machine at home, I thought you knew how to use linux?" ... which hurts a little ... hehe
 
Old 03-23-2016, 01:49 PM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Nice work nash-c and I do like the completeness of your answer for the future

Don't forget to mark your ticket as SOLVED
 
Old 03-23-2016, 03:06 PM   #19
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by nash-c View Post
or I still maintain that the original edit with the for loop does actually work too but perhaps with caveats:

Code:
for foo in $(ls -1 *.txt); do cat $foo | awk '{ if ($10 < -1000 && $10 >= -3000) print $0;}' >> filtered_$foo ; done
It probably works for the particular set of files you have, but there are a few common mistakes that would cause failures if you had filenames with spaces; put the code into http://www.shellcheck.net/ to see (except ignore SC2016, it's a false positive in this case).
 
Old 03-23-2016, 03:46 PM   #20
nash-c
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks again all for your patience and help.

I'll be doing more to teach myself how to learn awk (and programming in general) properly as well as looking into upgrading my version of awk. I use an iMac at work but I use Ubuntu/Linux PC at home so maybe I'll test the two against each other in future and see what difference it makes.

And I'm glad you like the "completeness" of my answer. I've been on bioinformatics forums before and many times I've been lost in threads and struggled to pull out the answer I needed. Hopefully this way it's clear for people.

Thanks and I'm sure I'll be back soon!!
 
Old 03-24-2016, 06:44 AM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
As a side note, Ubuntu normally defaults to mawk which also does not support all the same features as gawk which many on this site refer to when they say awk
 
  


Reply



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
split a file based on column value awk / sed? cusvenus Linux - Newbie 5 06-15-2013 04:16 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
[SOLVED] Sum numbers in a column in a specific block Alkass Programming 3 07-15-2011 01:12 PM
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 09:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:09 PM.

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
Open Source Consulting | Domain Registration