Reputation: 926
I'm trying to pipe two bash commands in R but I get a broken pipe error; any suggestion is appreciated. Here's 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: writing output: broken pipe With a short file it works properly. How can I work arround that please?
Thanks.
Upvotes: 5
Views: 5526
Reputation:
Alternatively, if your implementation of grep
supports it, you could use
grep -m 10 PATTERN FILE
Partial description from: man grep
on Ubuntu 12.04
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
This option was also available on my Red Hat 5.8 box where I was having a similar issue.
Upvotes: 2
Reputation: 3330
grep
is complaining because it has more output than 10 lines, and head
is cutting it off before it finishes.
I suggest hiding grep's stderr output (this is where the broken pipe error is printed).
system("grep 1 tmp.txt 2>/dev/null | head -n 10")
This won't work if you need to see other errors from grep; in that case, you will need a more complicated solution.
Upvotes: 7