简体   繁体   中英

running linux commands from R

I have a bunch of random files and I am going to run LINUX-file command on each file. Linux screen will be as follows

m7% file date-file.csv
date-file.csv: ASCII text, with CRLF line terminators
m7% file image-file.JPG
image-file.JPG: JPEG image data, EXIF standard

Only when Linux says that the file is a text file, I want to run a R script that goes through that file and finds all column names. In above screen, I want to run R script only on the first file. How could I achieve this conditional processing?

Is there any way I can run Linux commands from R? If i can do that then I can analyze the output given by Linux command to see if it contains text and then I can execute R script if required.

I am having difficulty achieving this and any help is appreciated

Try system()

08/27 7:08 [nodakai@kaidev01] ~/R$ R -q
> system("ls /")
bin  boot  dev  etc  home  initrd.img  initrd.img.old  lib  lib32  lib64  libGL.so  libnss3.so  lost+found  media  mnt  opt  proc  root  run  sbin  selinux  sftp  srv  sys  tmp  usr  var  vmlinuz  vmlinuz.old
> x = system("ls", TRUE)
> x
[1] "a.csv"                       "abi.pdf"                    
[3] "b.csv"                       "image.jpeg"                 
[5] "x86_64-pc-linux-gnu-library"
> for (f in system("ls", TRUE)) { if (length(grep("ASCII", system(paste("file", f), TRUE)))) { print(f) }  }
[1] "a.csv"
[1] "b.csv"

Globbing

If you're sure all files ending ".csv" are really plain text CSV files, just use Sys.glob()

> list.files()
[1] "a.csv"                       "abi.pdf"                    
[3] "b.csv"                       "image.jpeg"                 
[5] "x86_64-pc-linux-gnu-library"
> Sys.glob("*.csv")
[1] "a.csv" "b.csv"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM