Print first, second, every nth, and last record


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print first, second, every nth, and last record
# 1  
Old 09-02-2011
Print first, second, every nth, and last record

does anyone have an awk one-liner to:

print the first line, the second line, then every Nth line, and the last line of a file.

Thanks,
Kenny.
# 2  
Old 09-02-2011
I think this will work. Set n= to the desired value (9th line in the example):


Code:
    awk -v n=9 ' NR < 3  ||  !(NR % n)  { print; }'

You can always hard code the variable (doing it this way allows inclusion in a script with flexibility. You can also make it much more cryptic:


Code:
awk 'NR < 3 || !(NR % 9)'

# 3  
Old 09-03-2011
The following builds on agama's suggestion so that it handles the last line:
Code:
awk '{p=0; r=$0} NR<3 || !(NR%n) {print r; p=1} END {if (!p) print r}' n=9

p: flag to track whether a record has been printed.
r: most recent record

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 09-03-2011
Quote:
Originally Posted by alister
The following builds on agama's suggestion so that it handles the last line:

Very embarrassing -- I completely missed that requirement!! Thanks.
# 5  
Old 09-03-2011
Nah. Embarassing is what happened to me a couple days ago. I overlooked a requirement and incorrectly critiqued two other suggestions in the thread. Fortunately (I think), I was able to edit/delete my post before it was seen. Smilie
# 6  
Old 09-03-2011
But this doesn't print the last line. With GNU sed:
Code:
sed -n '${p;d};1,2{p;d};9~9p'

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to print nth till last column of ls output using sed

I wish to print first, third and sixth till the last column from the output of ls command ls -ltr /app/deploy.yml -rw-rw-r-- 1 user1 dba 27342 Aug 28 10:17 /app/deploy.yml Desired Output: Below command gives me the desired output. ls -ltr /app/deploy.yml | awk '{$2=$4=$5=""; print... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies

3. AIX

Print nth previous line after match

Please help me print nth line after match awk or sed one line command. (3 Replies)
Discussion started by: sushma123
3 Replies

4. Shell Programming and Scripting

Print nth line in a file

Bash/Oracle Linux 6.4 A basic requirement. How can I get nth line of a file printed ? Can I use grep in this case ? Example: In the below file, 12th line is "Kernel parameter check passed for rmem_max" . I just want the 12 line to be printed. # cat sometext.txt Kernel version check... (2 Replies)
Discussion started by: John K
2 Replies

5. UNIX for Dummies Questions & Answers

Print Nth to last field

Hey, I'm sure this is answered somewhere but my Googling has turned up nothing. I have a file with data in the following format: <desription of event> at <time and date>The desription of the event is variable length and hence when the list is displayed it is hard to easily see the date (and... (8 Replies)
Discussion started by: RECrerar
8 Replies

6. Shell Programming and Scripting

Print every nth line

Dear all, How to print every nth line. File like this: File input: 1 1 1 1 1 1 2 2 2 3 3 3 3 3 3 (3 Replies)
Discussion started by: attila
3 Replies

7. Shell Programming and Scripting

How to Print from nth field to mth fields using awk

Hi, Is there any short method to print from a particular field till another filed using awk? Example File: File1 ==== 1|2|acv|vbc|......|100|342 2|3|afg|nhj|.......|100|346 Expected output: File2 ==== acv|vbc|.....|100 afg|nhj|.....|100 (8 Replies)
Discussion started by: machomaddy
8 Replies

8. Shell Programming and Scripting

Get Nth record from last line

I have a growing file . I knew only last few lines which is constant for evey job run. I'd need to pull the Nth record from the last line. In this case i should not use search pattern. (2 Replies)
Discussion started by: ford2020
2 Replies

9. UNIX for Advanced & Expert Users

Print Full record and substring in that record

I have i got a requirement like below. I have input file which contains following fixed width records. 00000000000088500232007112007111 I need the full record and concatenated with ~ and characters from 1to 5 and concatenated with ~ and charactes from 10 to 15 The out put will be like... (1 Reply)
Discussion started by: ukatru
1 Replies

10. UNIX for Dummies Questions & Answers

Using sed to extract Nth record?

I have a comma-separated record and I'd like to use sed to pull the Nth record from it. It seems like it'd need to be something like this: sed -n 's/'"\,$1\,"'/&/p' Am I close? (3 Replies)
Discussion started by: doubleminus
3 Replies
Login or Register to Ask a Question