awk脚本从txt文件读取数据

时间:2018-10-20 17:07:23

标签: bash awk

我正在尝试创建awk脚本以从txt文件读取数据并创建输出脚本。可以使用awk创建输出吗?

while IFS=',' read -r a b c; do echo "source "$a" and target "$b" and line "$c"" ; done < test.txt

test.txt

"x","y","8"
"x","6"
"y","7"

输出:

source "x" and target "y" and line "8"
source "x" and target "6" and line 
source "y" and target "7" and line

预期输出:

"source x and target y and line 8"
"source x and line 6"
"target y and line 7"

2 个答案:

答案 0 :(得分:2)

我发现很难从提供的输入和输出中清楚地区分需求。但是,如果我们稍微调整一下您的输入(与Corentin的建议稍有不同,我们可以提供一个有效的awk脚本来提供所需的输出。

让我们从一个假设开始:

输入采用以下格式:

<source>,<target>,<line>

现在我们可以编写以下awk脚本:

awk -F, '
BEGIN { 
  split( "source target line" , value , " " ) 
}

{
  output=""
  for ( field = 1 ; field <=  length(value) ; field++  ) 
  {
    if ( $field ) {
      output = output  ( output ? " and " : "" ) value[ field ]   " " $field
    }
  }
  print "echo " output
}' << EOF
"x","y","8"
"x",,"6"
,"y","7"
EOF

说实话,以上代码略有遗漏标记。具体来说,我没有处理输入或输出中的双引号。我认为,如果我们谈论引号,则解决方案的可读性将会降低。从技术上讲,如果输入中的引号是为了保护逗号(例如CSV的情况),我会考虑使用具有csv支持的python(或其他语言)编写此程序。

这是上面程序的输出:

echo source "x" and target "y" and line "8"
echo source "x" and line "6"
echo target "y" and line "7"

答案 1 :(得分:1)

library(tidyverse)
library(mice)

complete(imp, 1) %>% 
  # generate age groups
  mutate(Age_Group = case_when(age >= 75 ~ "age > 75",
                               TRUE ~ "age <= 75")) %>% 
  # aggregate groups
  count(medication, Age_Group) %>% 
  # make the clean table
  spread(medication, n)