简体   繁体   中英

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

I have this C function that I am using to output any amount of integer that is input.

/*
  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;
}

In my shell the function works like this as expected with the pipe command

$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.

Here is my CSV file

$cat file.csv

Output:

 2,2,3,3,8

Using the sed command I remove the commas

$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

I get no output. I don't know if there is a syntax error, but I believe I should be able to do this with a csv file.

I say you have a BOM in "file.csv".

That will stop "./function" from scanning the first number.

$ 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

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