简体   繁体   English

在R中管道awk

[英]Piping awk in R

I am trying to read in this fixed-width file in R. I want to read in only the first 3 columns with lengths 2, 2 and 6, such that the second variable has the value "A". 我试图读取R中的此固定宽度文件 。我只想读取长度为2、2和6的前3列,以使第二个变量的值为“ A”。 Note that the second variable has two spaces but is actually only one character. 请注意,第二个变量有两个空格,但实际上只有一个字符。 I am using this command: 我正在使用此命令:

b = trim(read.csv(pipe("awk -v FIELDWIDTHS='2 2 6' -v OFS=',' '($2=='A '){ $1=$1 ''; print }'</path/rawk.txt"),header=F))

I am getting this error: 我收到此错误:

awk: cmd. line:1: ($2==A
awk: cmd. line:1:       ^ unexpected newline or end of string
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input

If I remove the empty space in 'A ': 如果我删除'A'中的空白:

b = trim(read.csv(pipe("awk -v FIELDWIDTHS='2 2 6' -v OFS=',' '($2=='A'){ $1=$1 ''; print }'</path/rawk.txt"),header=F))

I still get this error: 我仍然收到此错误:

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input

What am I doing wrong? 我究竟做错了什么?

This seems to work: 这似乎可行:

read.csv(pipe("awk -v FIELDWIDTHS='2 2 6' -v OFS=',' '($2==\"A \"){ $1=$1 \'\'; print }'<rawk.txt"),header=FALSE)


  V1 V2    V3
1 10 A  10001
2 10 A  10002
3 10 A  10003

But I'm not sure what the $1=$1 is all about... 但是我不确定$ 1 = $ 1到底是什么...

对于这种简单的内容,您是否可以更轻松地查看行的子字符串,而不是考虑其中的“实际”固定宽度字段?

b = trim(read.csv(pipe("awk 'substr($0,3,2)==\"A \"' /path/rawk.txt"),header=F))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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