Xargs, awk, match, if greater - as a one-liner


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Xargs, awk, match, if greater - as a one-liner
# 1  
Old 08-09-2016
Hammer & Screwdriver Xargs, awk, match, if greater - as a one-liner

Hi I have multiple files for which I want to use awk for the following:

Read each line in each file- if any of the columns match "PVALUE=" followed by the number, then print the line in case the number following "PVALUE=" is greater than 0.05.

I did the following:
Code:
ls *.txt | xargs -I @ -P15 sh -c "awk {if /PVALUE=(\d+)/)){if ($1 > 0.05){print $_}}}' @ - >@.fail"

but i get an error message:
Code:
sh: -c: line 0: syntax error near unexpected token `('


I appreciate suggestion in correcting the one liner for accommodating all of the above.

thanks
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments as required by Forum Rules.

Last edited by Don Cragun; 08-10-2016 at 12:51 AM.. Reason: Add CODE tags.
# 2  
Old 08-09-2016
doesn't the expression in the if statement have to be enclosed in square brackets?
# 3  
Old 08-09-2016
Should it be like this ? It still gives me an error message
Code:
ls *.txt| xargs -I @ -P15 sh -c "awk {if [/PVALUE=(\d+)/)]{if ($1 >=0.05){print $_}}}' @ - >@.fail"


Last edited by Don Cragun; 08-10-2016 at 12:52 AM.. Reason: Add CODE tags, again.
# 4  
Old 08-10-2016
I'm not at all sure that I understand what you're trying to do here. And saying you're getting an error message without showing us the exact error you're getting leaves us with a lot of guess work. But whatever you do is going to need matched single quotes surrounding your awk script, and xargs is not likely to replace @ inside a double quoted string with the filenames you seem to want.

But even after you get the quotes fixed (and getting rid of the unneeded sh -c and the extra set of double quotes needed for that, you still have several syntax errors in your awk script. Please show us a sample of the data in one of these text files and the output you are hoping this awk script will produce from that sample input file.

Your first post implies that there are multiple fields in your text files, and that you want to examine every column, so we also need to know that your text files are using as field delimiters.

And, especially since you're depending on non-standard options in the xargs utility you're using, we also need to know what operating system and shell you're using.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-10-2016
Hi Don
The following is the file sample.

Quote:
#row1
#row2
#row3
#row4
#row5
CHR POS INFO
chrX 10000 NRHITS=35;PVALUE=0.04
chrX 109000 NRHITS=6;PVALUE=
chrX 110000 NRHITS=10;PVALUE=0.6
chrX 120000 NRHITS=18;PVALUE=0.2
chrX 130000 NRHITS=39;PVALUE=0.035
I have edited the command as following:

Code:
ls *.txt | xargs -I {} -P15 sh -c awk {if [/PVALUE=(\d+)/){if ($1 >=0.05){print $_}}]}' {} >{}.fail

The following is the error message:
Quote:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `awk {if [/PVALUE=(\d+)/){if ( >= 0.05){print }}]}' tmp_file1.txt >tmp_file1.txt.fail'
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `awk {if [/PVALUE=(\d+)/){if ( >= 0.05){print }}]}' tmp_file2.txt >tmp_file2.txt.fail'
I am using the x86_64 GNU/Linux
# 6  
Old 08-10-2016
How about (untested):
Code:
awk 'match ($0, /PVALUE=[0-9.]*/) && substr($0, RSTART+7, RLENGTH-7) > 0.05' *.txt

# 7  
Old 08-10-2016
Thanks a lot.
Although tThe command works, individual output files are empty.No error message.

Code:
ls *.txt |xargs -I {} -P15 sh -c "awk 'match ($0, /PVALUE=[0-9.]*/) && substr($0, RSTART+7, RLENGTH-7) > 0.05' {} >{}.fail"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed one Liner inverse range match-help required

cat test.txt a c d e g (2 Replies)
Discussion started by: TomG
2 Replies

2. Shell Programming and Scripting

awk one liner

The below code is a simple modified sample from a file with millions of lines containing hundreds of extra columns xxx="yyy" ... <app addr="1.2.3.4" rem="1000" type="aaa" srv="server1" usr="user1"/> <app usr="user2" srv="server2" rem="1001" type="aab" addr="1.2.3.5"/>What's the most efficient awk... (2 Replies)
Discussion started by: cabrao
2 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

5. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

6. Shell Programming and Scripting

Awk one-liner?

Hello, I have two files... File #1 1 3 2 5 File #2 3 5 3 1 3 7 9 1 5 2 5 8 3 3 1 I need to extract all lines from File #2 where the first two columns match each line of File #1. So in the example, the output would be: 1 3 7 2 5 8 Is there a quick one-liner that would... (4 Replies)
Discussion started by: palex
4 Replies

7. UNIX for Dummies Questions & Answers

need an awk one liner

example input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

8. UNIX for Dummies Questions & Answers

awk one liner

I need a one liner to" find /pattern/ print from x lines before "pattern" to y lines after "pattern" (3 Replies)
Discussion started by: kenneth.mcbride
3 Replies

9. Shell Programming and Scripting

awk one liner

input a 100 200 300 b 400 10 output a 100 a 200 a 300 b 400 b 10 Thanx (6 Replies)
Discussion started by: repinementer
6 Replies

10. Shell Programming and Scripting

AWK greater than?

Sorry for such a basic question, but I have spent hours trying to work this out! I need an awk command (or similar) that will look at a text file and output to the screen if the 4th column of each line has a value greater than or equal to x. data.txt This is the 1 line This is the 2 line This... (6 Replies)
Discussion started by: dlam
6 Replies
Login or Register to Ask a Question