|
Home > Archive > Unix Shell > February 2007 > cat first line in all files in this dir
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
cat first line in all files in this dir
|
|
| Gary Wessle 2007-02-13, 7:18 pm |
| Hi
I have bunch of files in a dir, say 9 of them in all. I want to print
out the first line of each of the files along with the name of the
file. in zsh would be a bonus.
thanks alot
| |
| Xicheng Jia 2007-02-13, 7:18 pm |
| Gary Wessle wrote:
> Hi
>
> I have bunch of files in a dir, say 9 of them in all. I want to print
> out the first line of each of the files along with the name of the
> file. in zsh would be a bonus.
>
one way:
awk '{print FILENAME, $0; nextfile}' file*
Xicheng
| |
| Janis Papanagnou 2007-02-13, 7:18 pm |
| Gary Wessle wrote:
> Hi
>
> I have bunch of files in a dir, say 9 of them in all. I want to print
> out the first line of each of the files along with the name of the
> file. in zsh would be a bonus.
>
> thanks alot
Without name...
awk 'FNR==1' files...
With name...
awk 'FNR==1{print FILENAME,$0}' files...
Janis
| |
| Xicheng Jia 2007-02-13, 7:18 pm |
| On Feb 13, 4:47 pm, Gary Wessle <phd...@yahoo.com> wrote:
> Hi
>
> I have bunch of files in a dir, say 9 of them in all. I want to print
> out the first line of each of the files along with the name of the
> file. in zsh would be a bonus.
>
> thanks alot
with GNU grep:
grep -m1 '^' file*
Xicheng
| |
| Janis Papanagnou 2007-02-13, 7:18 pm |
| Xicheng Jia wrote:
> Gary Wessle wrote:
>
>
> one way:
>
> awk '{print FILENAME, $0; nextfile}' file*
Use 'nextfile' if you have gawk available, otherwise...
awk 'NR==1 {print FILENAME, $0}' file*
Janis
>
> Xicheng
>
| |
| Xicheng Jia 2007-02-13, 7:18 pm |
| On Feb 13, 5:02 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> Xicheng Jia wrote:
>
>
>
>
>
> Use 'nextfile' if you have gawk available, otherwise...
>
> awk 'NR==1 {print FILENAME, $0}' file*
>
Yes, you are right. using 'FNR' is much better for OP's case,
'nextfile' is only applicable under 'gawk' and Bell lab 'awk', not a
part of POSIX awk. thanks..
Regards,
Xicheng
| |
| Bill Marcum 2007-02-13, 7:18 pm |
| On 14 Feb 2007 08:47:38 +1100, Gary Wessle
<phddas@yahoo.com> wrote:
>
>
> Hi
>
> I have bunch of files in a dir, say 9 of them in all. I want to print
> out the first line of each of the files along with the name of the
> file. in zsh would be a bonus.
>
> thanks alot
head -n 1 *
--
If you can't understand it, it is intuitively obvious.
| |
| Stephane CHAZELAS 2007-02-14, 1:20 pm |
| 2007-02-14, 08:47(+11), Gary Wessle:
[...]
> I have bunch of files in a dir, say 9 of them in all. I want to print
> out the first line of each of the files along with the name of the
> file. in zsh would be a bonus.
[...]
Zsh specific (not to be used in shell scripts):
for file (*(ND-.)) IFS= read -re < $file
With GNU head:
head -qn1 -- /dev/null *(ND-.)
(beware that both read and head will return something even if
the file doesn't contain a full (terminated) line and that will
mess your output)
POSIXly:
for file in * .*; do
[ -f "$file" ] && head -n1 < "$file"
done
beware that both read and head will return something even for
files that don't contain a full (terminated) line and that will mess
your output
To work around it:
for file (*(ND-.)) {IFS= read -r < $file && print -r -- $REPLY}
--
Stéphane
| |
| blacksheep 2007-02-21, 7:16 pm |
| That should do the work:
find . -type f -exec head -1 {} \;
Regards,
p.
| |
| Kenny McCormack 2007-02-25, 1:25 am |
| In article <eri8dp$9se$1@aioe.org>, blacksheep <me@nospam.org> wrote:
>That should do the work:
>
>find . -type f -exec head -1 {} \;
>
>Regards,
>p.
Far better (than yours or any other posted in this thread so far):
gawk '{print;nextfile}' *
| |
| Stephane CHAZELAS 2007-02-25, 7:20 am |
| 2007-02-25, 03:56(+00), Kenny McCormack:
> In article <eri8dp$9se$1@aioe.org>, blacksheep <me@nospam.org> wrote:
>
> Far better (than yours or any other posted in this thread so far):
>
> gawk '{print;nextfile}' *
That ommits dot files doesn't descend into subdirectories, will
fail or have unexpected results if there are directories in the
current directory, will fail if there are non-readable files and
will miss the files whose name contains "=".
If you want to use awk (and you don't even need gawk);
find . -type f -exec awk '
BEGIN {
for (i = 1; i < ARGC; i++) {
if ((getline < ARGV[i]) > 0)
print
close(ARGV[i])
exit
}' {} +
--
Stéphane
| |
| Kenny McCormack 2007-02-25, 7:20 am |
| In article <slrneu2mvu.4km.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS <this.address@is.invalid> wrote:
>2007-02-25, 03:56(+00), Kenny McCormack:
>
>That ommits dot files (I think you missed a comma here, Ed)
The user can figure out for herself if that matters and act accordingly.
>doesn't descend into subdirectories,
This original spec says (direct quote):
cat first line in all files in *this* dir (emphasis mine, Ed)
I assume that explicitly excludes subdirs, so that's why I think the
"find" based "solutions" are wrong.
>fail or have unexpected results if there are directories in the
>current directory, will fail if there are non-readable files and
>will miss the files whose name contains "=".
The user can figure out for herself if any of that matters and act accordingly.
....
(Stupid, verbose shell-y solution [mercifully] snipped)
| |
| Ed Morton 2007-02-25, 1:17 pm |
| Stephane CHAZELAS wrote:
> 2007-02-25, 03:56(+00), Kenny McCormack:
>
>
>
> That ommits dot files doesn't descend into subdirectories, will
> fail or have unexpected results if there are directories in the
> current directory, will fail if there are non-readable files and
> will miss the files whose name contains "=".
>
> If you want to use awk (and you don't even need gawk);
>
> find . -type f -exec awk '
> BEGIN {
> for (i = 1; i < ARGC; i++) {
> if ((getline < ARGV[i]) > 0)
> print
> close(ARGV[i])
> exit
> }' {} +
>
>
I can't find the reference right now, but I recall reading that some
awks (not gawk) will exit if getline fails to open a file for reading.
Ed.
| |
| Kenny McCormack 2007-02-25, 1:17 pm |
| In article < 8pGdnXjHkINFEHzYnZ2dnUVZ_hCdnZ2d@comcast
.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
(some clueless clown tried)
>
>I can't find the reference right now, but I recall reading that some
>awks (not gawk) will exit if getline fails to open a file for reading.
>
> Ed.
True. But I think the real point is that only a moron would code it
like the above anyway (embedding getline inside BEGIN is almost always a
display of AWK ignorance).
| |
| Stephane CHAZELAS 2007-02-25, 1:17 pm |
| 2007-02-25, 15:03(+00), Kenny McCormack:
[...]
[...][vbcol=seagreen]
> True. But I think the real point is that only a moron would code it
> like the above anyway (embedding getline inside BEGIN is almost always a
> display of AWK ignorance).
I take it gawk authors are morons then as that solution is given in
gawk's manual to work around the limitation that makes gawk exit
on the first open error.
And as far as I can tell, given that "nextfile" is not standard
awk, that's the only standard way to do it without having to
read the whole files.
The other non-awk solutions given, although not as good to your
mind, had fewer limitations, that was what I was pointing out,
no need to be rude.
--
Stéphane
| |
| Stephane CHAZELAS 2007-02-25, 1:17 pm |
| 2007-02-25, 07:23(-06), Ed Morton:
[...]
> I can't find the reference right now, but I recall reading that some
> awks (not gawk) will exit if getline fails to open a file for reading.
[...]
Those awks wouldn't be standard then. Same for "ARGV", some awks
don't know it, but then they are not POSIX awks.
--
Stéphane
| |
| Kenny McCormack 2007-02-25, 1:17 pm |
| In article <slrneu3af7.7oo.stephane.chazelas@spam.is.invalid>,
Stephane CHAZELAS <this.address@is.invalid> wrote:
>2007-02-25, 07:23(-06), Ed Morton:
>[...]
>[...]
>
>Those awks wouldn't be standard then. Same for "ARGV", some awks
>don't know it, but then they are not POSIX awks.
All of which is irrelevant if it breaks on the user's machine.
At least in theory, the purpose of posting here is help people, not to
argue about "standards".
| |
| Janis Papanagnou 2007-02-25, 7:16 pm |
| Kenny McCormack wrote:
> In article <slrneu3af7.7oo.stephane.chazelas@spam.is.invalid>,
> Stephane CHAZELAS <this.address@is.invalid> wrote:
>
>
>
> All of which is irrelevant if it breaks on the user's machine.
True.
>
> At least in theory, the purpose of posting here is help people, not to
> argue about "standards".
I haven't seen the charter of c.u.s but disputes or hints about
standards and portability (though in some contexts overemphasized)
seems to me to be an important part of the discussion here.
Janis
| |
| Will Renkel 2007-02-26, 1:18 pm |
| how about...
head -1 `ls -A`
|
|
|
|
|