Sponsored Content
Full Discussion: print first and last row
Top Forums UNIX for Dummies Questions & Answers print first and last row Post 302706475 by bakunin on Wednesday 26th of September 2012 09:54:16 PM
Old 09-26-2012
sparks partly got it right:

Code:
sed -n '1p;$p'

will print the first and last line of whatever comes its way: in a pipeline it prints the first and the last line of the input, given a filename it prints the first and last line of the file.

However: "sort -n" sorts numerically and with the filenames this does simply nothing because the aren't numerical values. The same goes for "sort -nk9", which sorts numerically on non-numerical input.

"ls" built-in default sorts ascending alphabetically, unless it is told other to do otherwise, like with "-r" (reverse the sorting order) and/or the "-t" (sort on timestamp). To switch off alphabetacal sorting to sort on timestamp descending ("-tr") first only to re-sort alphabetically later is somewhat futile, isn't it?

Code:
ls | sed -n '1p;$p'

will do what you want. You might ask why the output is in one column where the output of "ls" is in several columns. "ls" formats the output only in several columns if its output is going to a terminal. Still, you can force one-column-output with the "-1" switch and if you prefer long output use the "-l" switch. Write

Code:
ls -1 | sed -n '1p;$p'
ls -l | sed -n '1p;$p'

If you want to make sure to have only files (not directories, block devices, links, etc.) in your output you can filter them out in the long output easily:

Code:
ls -l | sed -n '/^-/p' | sed -n '1p;$p'

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print out a row of data

hi im completely new at unix so dont hate me for asking what is probably the easiest question ever. below is an extract of some data im processing. the first column is like a counter while the second is an ip address. i need to be able to output the ip address based on which has the largest... (4 Replies)
Discussion started by: Streetrcr
4 Replies

2. Shell Programming and Scripting

only print a selected row

this works: cat file.txt| awk 'NR==45,NR==55' but how do I assign variables instead of numbers: this does not work: cat file.txt | awk 'NR==$start,NR==$end' there need variables instead of numbers Sorry for my English Thank you for answer (3 Replies)
Discussion started by: gizmo16
3 Replies

3. Shell Programming and Scripting

Print the row element till the next row element appear in a column

Hi all I have file with columns F3 pathway CPS F2 H2 H4 H5 H6 no pathway CMP H7 H8 H9 H10 My expected output is F3 pathway CPS F2 pathway CPS (10 Replies)
Discussion started by: Priyanka Chopra
10 Replies

4. UNIX for Dummies Questions & Answers

Print last row of numbers

I have a spreadsheet of extremely long rows of numbers. I want to print only the last column. Tried using printf but there seems to be too many rows. example: 3 100 34 7 23 0 8 ..... X 400 203 778 1 ..........Y 58 3 9 0 100 ..........Z I only want to print X, Y and... (1 Reply)
Discussion started by: jimmyf
1 Replies

5. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

6. Shell Programming and Scripting

Print various rows in one row

I have this in a file 11.22.33.44 yyyyyyuser With awk/sed, I need this to be output as follows alias server.domain.com='ssh yyyyyyuser@11.22.33.44' (4 Replies)
Discussion started by: anil510
4 Replies

7. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

8. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

9. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

10. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies
evmsort(1)						      General Commands Manual							evmsort(1)

NAME
evmsort - sort events SYNOPSIS
show_template]] sport_spec] [filename | DESCRIPTION
The command reads and sorts a stream of EVM events. By default, events are read from and are sorted into ascending timestamp order. Sorted events are written to the command's stream in the form of raw EVM events. The sort order can be specified by supplying a sort_spec, either through the option or by setting the environment variable to the required specification. The specification is a character string of the form: key_item[ The key_item is the name of any EVM standard data item and the character is optional. If the special key_item is specified, the event's data item is expanded as described in the evmshow(1) manpage, and the resulting string is used as the sort key. The optional trailing or character indicates ascending or descending sort. By default, items are sorted in ascending order. Multiple items in the string are separated by colon characters The left-most key_item in the specification is the most significant key. If a key_item that is included in the sort specification is not present in an event read from the input stream, the item is considered to have a low value for that event. For example, if input events are being sorted in ascending order according to their data items, and one event does not contain a name, that event appears first in the output stream. If the option is specified, automatically starts and pipes the sorted event stream into it, producing a sorted and formatted display of events. If a argument is also specified with the specified template string is passed to through its own option. See the evmshow(1) manpage for more information. Options Automatically starts to produce a formatted display of the sorted events. The specification to be used in sorting events. See the section for the syntax of the sort_spec, and for details of the default sort order. Template string to be passed to if the option is also specified. If the option is not specified, this argument is ignored. See the evmshow(1) manpage for a description of the template string. Operands filename Reads event sources from filename. If filename is omitted, or is specified as event sources are read from EXTERNAL INFLUENCES
Environment Variables The following environment variables affect the execution of This variable contains a string formatted as described for a sort_spec. If the option is omitted, the string is used as the sort_spec. RETURN VALUES
The following exit values are returned: Successful completion. An error occurred. EXAMPLES
o The following example retrieves all events with a priority of 300 or greater, sorts them into descending order of priority (most urgent event first) and then ascending order of time (oldest event first), and automatically runs to display them. o The following example yields a list of all events, sorted with the latest event first within o The following example yields a list of all events, sorted first in order of their formatted messages and then sorted in ascending order of time. Events are passed to for display and subsequently to the command for viewing. WARNINGS
The command rejects attempts to output raw events to a terminal device. SEE ALSO
Commands evmget(1), evmpost(1), evmshow(1), evmwatch(1). Files evmchannel.conf(4). Event Management EVM(5). EVM Events EvmEvent(5). Event Filter EvmFilter(5). evmsort(1)
All times are GMT -4. The time now is 04:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy