If you look at the output of find -name "data.csv"
, you'll see that it prints the full pathnames of files named data.csv
. The pipeline passes that on to sed
, which prints the second line of its input.
So what you're saying is, "Here's a list of files. Give me the second one one the list." What you really want to say is "Here's a list of files. For each one, give me the second line." And for that, you want to use xargs
.
Most likely, you'll need
find -name "data.csv" -print0 | xargs -0 -n 1 sed -n 2p > final.csv
It's often a good idea to use find -print0 | xargs -0
: the -print0
causes find
to use NUL characters as separators between filenames instead of a newline, and -0
tells xargs
to expect this. This prevents filenames with spaces, returns, or other weird characters from messing up your pipeline.
The -n 1
tells xargs
to run a separate sed
process for each "data.csv" file it finds, rather than trying to batch them together, which usually makes things more efficient. In this case, if you run
sed -n 2p file1 file2 file3
it'll internally concatenate all of its input files into one input stream, and print the second line of that. But RTFM: there may be a way to make sed
not do that, that I've missed.
\n
"? The shell sees that as "n
", and passes it on tosed
, which interprets that as the name of an input file. And then, as you see, it complains that there's no file or directory called "n".