How to get first line of output from command line?

5

I'm running a find command that's returning multiple results, but I only need the first result. A bit of googling led me to the "read" command, but I couldn't figure it out, and the man page didn't prove too helpful.

asdfasdfasdfasdfasdfasdfasdf

Posted 2014-08-08T20:16:37.393

Reputation: 83

Why not just pipe the results to a file? (i.e $ mycommand > myfile.txt) – Oxcug – 2014-08-08T20:19:59.213

@theMonster - that's not a pipe. That's redirecting the standard output to a file. A pipe would involve... a pipe. '|' e.g. ls -l | head -1 - as answered by Jeff Clayton. Pipes are a pseudo file, though you can make them a file, using mkfifo. – JezC – 2014-08-09T14:14:18.287

Answers

10

Enter your command (example: ls -l) then the head command with a pipe like so:

ls -l | head -1

Jeff Clayton

Posted 2014-08-08T20:16:37.393

Reputation: 918

2to get the top 10 lines you would change that to head -10 – Jeff Clayton – 2014-08-08T20:26:22.880

in your case it would be this... – Jeff Clayton – 2014-08-08T20:27:03.783

find (command line arguments here) | head -1 – Jeff Clayton – 2014-08-08T20:27:38.960

head -n 10 - ref man head – Hannu – 2014-08-08T20:29:07.823

I am using OS X as he mentioned and head -1 works (head -10 as well) no extra args needed – Jeff Clayton – 2014-08-08T20:30:43.440

Yes it works, but it is not documented to work - which means it will eventually break / stop working. ':-] - also try head file1 fil2 file3 -n 1 with and without -n. – Hannu – 2014-08-08T20:38:06.820

I used it 20 years ago in college, thinking it is not going to change any time soon (we used sun servers) – Jeff Clayton – 2014-08-08T20:41:42.657

Yep, it has worked for long, I was using it in the mid 90's too, ;-) – Hannu – 2014-08-08T20:43:41.270

to be completely correct, if you are going to use a script, you will want to use paths as well. on os x (i am using mavericks) it is /usr/bin/find (arguments) | /usr/bin/head -n 1 (-n added to make Hannu happy ;) – Jeff Clayton – 2014-08-08T20:48:07.827

=) that is what $PATH is for ( LOL, I'm going to bed now! ) – Hannu – 2014-08-08T20:55:31.743

you cannot always rely on the environment in the path variable. major security flaw if you leave it out. hackers put their own command with same name in the path... also, if you run it as a cron job or other server command it will not have the local path that a user will. – Jeff Clayton – 2014-08-08T20:59:09.243

many times i have seen (and even had happen to me) a cron job not working, because it relied on a path that was not set the same as the command line. that and being a sysadmin it was one of the first things instructed as the right way to do things. – Jeff Clayton – 2014-08-08T21:01:26.240

Awesome, that does exactly what I needed. I didn't even know about the head command's existence. I also appreciate the banter between you two ;) Thanks for the help. – asdfasdfasdfasdfasdfasdfasdf – 2014-08-08T22:16:58.787

You can also get the last line by trading it with the tail command (use it exactly the same way) – Jeff Clayton – 2014-08-08T22:19:08.337

Here is a fun one... say you wanted the second line only... you can use them in sequence. you would do it like this: find (stuff) | head -n 2 | tail -n 1 (clearly you could get a block of lines in order this way -- great for specific logging) – Jeff Clayton – 2014-08-08T22:22:36.443

reversing those you could get the second to last line... or even say lines 2-7 of a file (play with it, it is cool stuff) – Jeff Clayton – 2014-08-08T22:24:29.703

Very cool. That could definitely prove useful in the future. Thanks again. – asdfasdfasdfasdfasdfasdfasdf – 2014-08-08T22:36:15.310

More: head -n -x all but the x last lines, tail -n +x the remainder of the lines, starting at line x - more in the ABS guide referred to in my answer with info on read. – Hannu – 2014-08-09T09:14:57.140

0

You can use the more command to control the amount of text that is shown at a time.

To solve your problem :

Redirect your output to a file you create.

Use For Loop with 1 cycle (counter = 1 , -1 each time). You need to use tokens for search function.

The For Loop will ECHO the first command.

Delete your File.

Store the echo of the FOR Loop in a variable (optional).

I can give some usage definitions if you need it.

Uncreative Name

Posted 2014-08-08T20:16:37.393

Reputation: 64

0

How to use read

$ echo -e "1\n2\n3\n4\n5"
1
2
3
4
5

$ echo -e "1\n2\n3\n4\n5" | while read n; do  echo "n:$n" ; done
n:1
n:2
n:3
n:4
n:5

$ 


More info on how bash works is available at www.tldp.org in Advanced Bash-Scripting Guide,
there is one more guide on bash Bash Guide for Beginners.

Hannu

Posted 2014-08-08T20:16:37.393

Reputation: 4 950

0

I like lots of ways to tackle unix problems. Here's some others, assuming that command is replaced by whatever it is you're actually doing:

command | awk 'NR == 1 {print}'

command | sed -e 1q

TMP=`mktemp tempXXX`; command > ${TMP} ; ed ${TMP} << HERE
1p
HERE

command | split -1 - ; cat xaa ; rm -f x[a-z][a-z]

LINECOUNT=$(command | tee tempfile | wc -l | sed -e 's/ *//g'); \
  tail -$((${LINECOUNT} - 1)) tempfile | diff -u - tempfile | grep '^+' | \
  grep -v '^+++' | sed -e 's/^+//'

If your output is fixed length records (which may be true; example is for 80 characters of input plus a terminal newline):

command | dd ibs=81 count=1

At one point I worked on installing Unix systems from capacity constrained storage devices (floppy disks). We often had to find imaginative ways to use the limited set of commands that we could include on a 1.44MB floppy :)

JezC

Posted 2014-08-08T20:16:37.393

Reputation: 550