Pipeline of two bash commands in R: pipe break error

I try to execute two bash commands in R, but I get an interrupt error message; Any suggestion is welcome. This is where I am:

#Create a long file (2GB on your drive...) write.csv(rep(1,1E8),file="long.txt", row.names=FALSE) system("grep 1 tmp.txt") #This works system("grep 1 tmp.txt| head -n 10") #This gives a broken pipe error 

I get grep: output record: broken pipe With a short file, it works correctly. How can I work with the arch, please?

Thanks.

+4
source share
2 answers

grep complains because it has more output than 10 lines, and head disables it until completion.

I suggest hiding the grep stderr output (the broken pipe error is printed here).

 system("grep 1 tmp.txt 2>/dev/null | head -n 10") 

This will not work if you need to see other errors from grep; in this case, you will need a more complex solution.

+6
source

Alternatively, if your grep implementation supports it, you can use

 grep -m 10 PATTERN FILE 

Partial description: man grep on Ubuntu 12.04

 -m NUM, --max-count=NUM Stop reading a file after NUM matching lines. 

This option was also available in my Red Hat 5.8 box, where I had a similar problem.

+2
source

All Articles