繁体   English   中英

How to pipe the output of a sed command into a function in unix

[英]How to pipe the output of a sed command into a function in unix

我有这个 C function 用于 output 任何数量的 Z157DB7ZDF53600235728E51 输入。

/*
  This program will be called function.c
*/

#include <stdio.h>

int main (void) {

   int num;

   while(scanf("%d",&num)==1) {
     printf("You entered: %d\n",num);
   }

   return 0;
}

在我的 shell 中,function 的工作方式与 pipe 命令的预期一样

$echo "1 7 3 " | ./function

Output:

You entered: 1
You entered: 7
You entered: 3

Now what I'm trying to do is use the sed command on a csv file and pipe the output into my function.

这是我的 CSV 文件

$cat file.csv

Output:

 2,2,3,3,8

使用sed命令我删除逗号

$sed 's/,/ /g' file.csv

Output:

2 2 3 3 8

Now the issue I have is when I try to use the output of the sed command to pipe the numbers into my function:

$sed 's/,/ /g' file.csv | ./function

我没有得到 output。 我不知道是否存在语法错误,但我相信我应该能够使用 csv 文件来做到这一点。

我说你在“file.csv”中有一个BOM

这将阻止“./function”扫描第一个数字。

$ hexdump -C file.csv
00000000  ef bb bf 32 2c 32 2c 33  2c 33 2c 38 0a           |...2,2,3,3,8.|
0000000d

$ sed 's/,/ /g' file.csv |hexdump -C
00000000  ef bb bf 32 20 32 20 33  20 33 20 38 0a           |...2 2 3 3 8.|
0000000d

$ cat file.csv
2,2,3,3,8

$ cut -b4- file.csv |sed 's/,/ /g' |./function
You entered: 2
You entered: 2
You entered: 3
You entered: 3
You entered: 8

$ sed 's/,/ /g' file.csv |cut -b4- |./function
You entered: 2
You entered: 2
You entered: 3
You entered: 3
You entered: 8

暂无
暂无

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

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