Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get line number with bash in R

Tags:

bash

r

system

using

system(paste("wc -l file_1.txt"))

in R to obtain the line number of a file The output is

1601 file_1.txt

My problem is that if I type system(paste("wc -l file_1.txt"))->kt and then

kt
[1] 0 

I would need to be able to say whether

system(paste("wc -l file_1.txt"))->kt
kt[1]==1600 

or not..but I cant access the elements from the system commadn or the printout...how can i do that to somehow check whether the file has 1600 lines without reading it into R first...

like image 409
heinheo Avatar asked May 29 '15 14:05

heinheo


People also ask

How do I show line numbers in Bash?

In Bash, $LINENO contains the line number where the script currently executing. If you need to know the line number where the function was called, try $BASH_LINENO . Note that this variable is an array. See here for details of Bash variables.

How do I print a line in Bash?

Printing Newline in Bash The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way. However, it's also possible to denote newlines using the “$” sign.

How do I read a file line in Bash?

Method 1: Using read command and while loop We can use the read command to read the contents of a file line by line. We use the -r argument to the read command to avoid any backslash-escaped characters.


1 Answers

system only returns the return value of your command by default, you need to use its intern argument:

system(paste("wc -l banner.p"), intern=T)->kt

kt would then be some string like

<lines> <filename>

And then you could parse the string.

like image 131
zw324 Avatar answered Sep 25 '22 11:09

zw324