Reputation: 1523
I am on Mac Os 10.14.6 and have a directory that contains hundreds of text files.
In the terminal, I would like to display a list of the first line of every file. How can I do this?
Steps I have tried
Googling around, I think the solution is to use a command called head
. This is the command I have tried:
head -n 1 *
However, it produces output like this:
==> Minutes.txt <==
Minutes of the meeting
==> Daily Report.txt <==
Daily Report for Feb
==> Important incidents.txt <==
Incidents that happened yesterday
I don’t want the File 1.text
headings. I just want it to be a list like this:
Minutes of the meeting
Daily Report for Feb
Incidents that happened yesterday
According to this page, you can use any one of the following commands to remove the headers:
-q, --quiet, --silent
So I revised my command as follows:
head -n1 -q *
But it produces the following error message:
head: illegal option -- q
usage: head [-n lines | -c bytes] [file ...]
How can I display a list of the first line of every file without the header. The solution doesn't have to use head
although from my research, that seems to be the best approach.
Upvotes: 1
Views: 8033
Reputation: 34324
I'm guessing your version of head
doesn't support the -q
option; what does man head
(run on your Mac) show?
A small awk
solution:
awk '{ print ; nextfile }' *
As a file is processed the first line is printed, then we skip to the next file; net result is that we only print the first line of each file.
Keep in mind this could generate some odd results depending on what matches *
... binary files? directories?
Upvotes: 2
Reputation: 43
I hope the grep version should be acceptable in Mac, as explained in the 2nd answer of this discussion (https://askubuntu.com/questions/701514/how-to-select-the-first-line-from-each-file-in-a-directory-and-print-it-into-a-n)
grep -m 1 '.' *.txt >output.file
grep will match any character and will exit after the first match i.e. grep will output the first lines of all the input files and we are saving those in out.txt.
[*it is just the copy/paste of the above link.]
For skipping/ignoring the 1st line, this can be modified like this:
grep -m 1 "^[^=]" *.txt # This should skip the first line
The first ^ refers to the beginning of the line, so lines with comments starting after the first character will not be excluded. [^=] means any line that does not start with '='.
Upvotes: 0
Reputation: 1934
I don't have a Mac to test this on, but I think these are two options.
Option 1. Use tail to get the last line of the output from head.
If you iterate over each file you can use tail -n 1
to trim the output from head
e.g.:
for FILENAME in *; do head -n 1 $FILENAME | tail -n 1; done
If you don't want to type that repeatedly you can define a function e.g.
line1() { for FILENAME in "$@"; do head -n 1 $FILENAME | tail -n 1; done }
Then call the function as line1 *
.
You can put this in your .bashrc to keep it available after opening a new terminal.
Option 2. Install coreutils
The -q
option doesn't work because some operating systems use different executables from the gnu core utilities. You should be able to install the gnu-coreutils with:
brew install coreutils
That should give you access to ghead
the gnu implementation of head, which should support the -q
option.
There are also options to have coreutils override the default system utilities, but since I don't have a Mac I can't confirm how to do it.
Here's an answer that suggests how to do it: https://apple.stackexchange.com/questions/69223/how-to-replace-mac-os-x-utilities-with-gnu-core-utilities#answer-69332
It suggests you add the utilities up-front in your path:
PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
As with the function solution above you may need to add this to .bashrc to make it persistent across terminal sessions.
Upvotes: 1