Visit The New, Modern Unix Linux Community


Removing top few and bottom few rows in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing top few and bottom few rows in a file
# 1  
Removing top few and bottom few rows in a file

Hi,
I have a requirement where I need to delete given number of top and bottom rows in a flat file which has new line as its delimiter.

For ex: if top_rows=2 & bottom_rows=1

Then in a given file which looks like:

Code:
New York
DC
LA
London
Tokyo
Prague
Paris
Bombay
Sydney
Singapore

The resulting output should look like:

Code:
LA
London
Tokyo
Prague
Paris
Bombay
Sydney



Any inputs appreciated.
Thanks.

Last edited by Scrutinizer; 06-12-2015 at 04:18 PM.. Reason: CODE tags
# 2  
Try:
Code:
awk 'NR>a+b{print A[NR%b]} {A[NR%b]=$0}' a=2 b=1 file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk 'NR>a+b{print A[NR%b]} {A[NR%b]=$0}' a=2 b=1 file

Thanks for the solution. It works as long as the a and b doesnt equal to 0. It is possible that we may not have to delete any top and bottom records and in that case the value for top and bottom rows will be 0.
# 4  
So, try:
Code:
awk 'NR>a+b{print (b>0 ? A[NR%b] : $0)} b>0{A[NR%b]=$0}' a=2 b=1 file

This User Gave Thanks to Don Cragun For This Post:
# 5  
Quote:
Originally Posted by Don Cragun
So, try:
Code:
awk 'NR>a+b{print (b>0 ? A[NR%b] : $0)} b>0{A[NR%b]=$0}' a=2 b=1 file

Thanks it works great.
# 6  
With bash there is no need for any external command:

Code:
#!/bin/bash
file=$1

mapfile -t xx < "$file"
n=${#xx[@]}
printf "%s\n" "${xx[@]:1:n-2}"

These 3 Users Gave Thanks to cfajohnson For This Post:
# 7  
Nice! To remove the top lines as well as requested, try
Code:
top=2
bot=1
printf "%s\n" "${xx[@]:$top:n-$top-$bot}"
LA
London
Tokyo
Prague
Paris
Bombay
Sydney


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #25
Difficulty: Easy
Google was the first search engine developed for indexing the Internet.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove top and bottom for each column

Dear All I was wondering if someone could help me in resolving an issue. I have a file like this: column1 column2 2 4 3 5 8 9 0 12 0 0 0 0 9 0 87 0 1 0 1 0 1 0 4 0 (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

2. Shell Programming and Scripting

Removing Duplicate Rows in a file

Hello I have a file with contents like this... Part1 Field2 Field3 Field4 (line1) Part2 Field2 Field3 Field4 (line2) Part3 Field2 Field3 Field4 (line3) Part1 Field2 Field3 Field4 (line4) Part4 Field2 Field3 Field4 (line5) Part5 Field2 Field3 Field4 (line6) Part2 Field2 Field3 Field4... (7 Replies)
Discussion started by: ekbaazigar
7 Replies

3. Shell Programming and Scripting

How to print from bottom to top?

Hi, i have a file which contains PID and wanted to execute kill command. but the thing is, when killing PID's needs to kill PID from bottom to top. Please help INPUT 21414 sh -c extract.ksh ASA 21416 /bin/ksh extract.ksh ASA 21428 /usr/bin/perl -w /var/tmp/tempperl.21416 ASA... (4 Replies)
Discussion started by: reignangel2003
4 Replies

4. Ubuntu

Title bar top and bottom

Hello forum, Seems that only I have alot of questions regarding Ubuntu :D In Ubuntu 12.04 LTS the gnome I have been using gdm and lightdm. In lightdm the top and side bars are aka "unity" and can be removed using apt-get remove unity I need to do the same for menu bars gdm. I do not... (0 Replies)
Discussion started by: br1an
0 Replies

5. Shell Programming and Scripting

Search for string and print top and bottom line

Hi Folks I need a one liner to parse through a log and if the string is found print the line above, the line with the string and the line below. example: The ball is green and blue Billy through the ball higer. Jane got hurt with the ball. So if I search for Billy I would need the 3... (1 Reply)
Discussion started by: bombcan
1 Replies

6. Shell Programming and Scripting

Removing rows from a file

I have a file like below and want to use awk to solve this problem. The record separator is ">". I want to look at each record section enclosed within ">". Find the row with the 2nd and 3rd columns being 0, such as 10 0 0 I need to take the first number which in this case is 10. Then... (15 Replies)
Discussion started by: kristinu
15 Replies

7. Shell Programming and Scripting

Get the bottom line of a file to the top of the file

Hi, i have a small requirement where i have to get the bottom most line from a file to the topmost position. a small example is shown below.. $ cat beep.txt It is first documented as being played in southern England. In the 16th century. By the end of the 18th century, Cricket is a... (5 Replies)
Discussion started by: Shellslave
5 Replies

8. Shell Programming and Scripting

How to search for a pattern from bottom to top.

Hi, I have a file, which is having a pattern "SEARCH" somewhere towards end of the file, if i am giving " grep -i "SEARCH" $File" , it is taking too much time as file is very big. So i want to search for the pattern from the back side of the file, how can we search for a pattern in bottom... (5 Replies)
Discussion started by: Prat007
5 Replies

9. Shell Programming and Scripting

grep for a particular pattern and remove few lines above top and bottom of the patter

grep for a particular pattern and remove 5 lines above the pattern and 6 lines below the pattern root@server1 # cat filename Shell Programming and Scripting test1 Shell Programminsada asda dasd asd Shell Programming and Scripting Post New Thread Shell Programming and S sadsa ... (17 Replies)
Discussion started by: fed.linuxgossip
17 Replies

Featured Tech Videos