unix - Why does sed only show one line though pipe -


i have several txt files under directory, , want see first line of every file

so use

ls *txt | xargs sed -n '1p' 

however returns 1 line of first file

what wrong?

p.s.: know can use head, ask why sed not working

use argument -t xargs see going on:

ls *txt | xargs -t sed -n '1p' 

you see sed run as:

sed -n '1p' foo.txt bar.txt gar.txt 

and sed supports 1 input file, print first line of file foo.txt , exit.

xargs assuming want pass list of input files together.

to tell pass 1 @ time, need use -l num option, tells xargs pass 1 line @ time ls command. [note there other issues run if file name has blank in it]

ls *txt | xargs -l 1 -t sed -n '1p' 

you see:

sed -n '1p' foo.txt  sed -n '1p' bar.txt  sed -n '1p' gar.txt 

in unix there many ways task; other ways include:

(if use /bin/csh or /bin/tcsh):

foreach f (*txt) echo -n $f: sed -1p $f end 

if use /bin/sh or /bin/ksh, then:

for files in *txt;   echo -n $files : sed -n '1p' $files done 

also consider using program find; lets qualify types of files want @ , can recursively examine sub directories:

`find . -type f -a -name "*txt" -print -a -exec sed -n '1p' {} \;` 

Comments