To return the second line of output, do the following:
... | sed -ne 2p
And use it as a function:
function print_2nd_line { sed -ne 2p } mdfind 'my_search_string' | print_2nd_line
You can also choose shorter names, such as p2
, as you wish.
The function can also be configured to be able to print the second line from the specified files, for example:
function print_2nd_line { sed -ne 2p -- " $@ " } print_2nd_line file ... | print_2nd_line
By the way, a more efficient version will be
sed -ne '2{p;q}'
UPDATE
As suggested by Charles Duffy, you can also use this format for compatibility with POSIX. In fact, it is also compatible with all shells based on the original V sh system.
print_2nd_line() { sed -ne '2{p;q}' -- " $@ " }
In addition, if you want to pass a custom line number to your function, you can:
print_2nd_line() { N=$1; shift sed -ne "${N}{p;q}" -- " $@ " }
Where you can run it like:
... | print_2nd_line 2
or
print_2nd_line 2 file
source share