Visit The New, Modern Unix Linux Community


Remove x lines form top and y lines form bottom using AWK?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove x lines form top and y lines form bottom using AWK?
# 1  
Remove x lines form top and y lines form bottom using AWK?

How to remove x lines form top and y lines form bottom.

This works, but like awk only
Code:
cat file | head -n-y | awk 'NR>(x-1)'

so remove last 3 lines and 5 first
Code:
cat file | head -n-3 | awk 'NR>4'

# 2  
Instead of awk

This removes first 4 lines (4+1) and removes last 3 lines
Code:
head -n-3 file | tail -n +5

OR
Code:
tail -n +5 file | head -n -3

You have to add +1 to the tail part..

EDIT:

with single awkSmilie

Code:
awk -v top="$x" -v bottom="$y" 'NR > top{j++;if(j > bottom){j=1};if(A[j]){print A[j];A[j]=$0}else{A[j]=$0}}' file


Last edited by pamu; 11-01-2012 at 06:09 AM.. Reason: added awk part
This User Gave Thanks to pamu For This Post:
# 3  
Code:
## NAME: topntail
## USAGE: topntail [-b N] [-e N] [FILE ...]

b=1  ## No. of lines to remove from beginning
e=1  ## No. of lines to remove from end

## Parse command-line options
while getopts b:e: opt
do
  case $opt in
      b) b=$OPTARG ;;  ## Number of lines to remove from beginning
      e) e=$OPTARG ;;  ## Number of lines to remove from end
  esac
done
shift $(( $OPTIND - 1 ))

case $b$e in   ## check for non-numeric characters in $b and $e
    *[!0-9]*) exit 5 ;;
esac

if [ $e -eq 0 ]
then
  sed "1,${b}d" "$@"  ## just remove from the top
else
  ## The buf[] array is a rotating buffer which contains the last N lines
  ## where N is the number of lines to be removed from the bottom of the file
  ## Printing starts when the line number is equal to the sum of the number
  ## of lines to be removed from top and bottom, and continues to the end
  ## of the file; the earliest line in the buffer is printed.
  ## The last N lines will not be printed.
  awk 'NR > b + e { print buf[ NR % e ] }
                  { buf[ NR % e ] = $0 }' b=$b e=$e "$@"
fi

This User Gave Thanks to cfajohnson For This Post:
# 4  
Awk version:
Code:
awk 'NR>y+x{print A[NR%y]} {A[NR%y]=$0}' x=5 y=3 file

---
EDIT: OK I see the exact same solution is already part of cfajohnsons script, my bad...
# 5  
Quote:
Originally Posted by Scrutinizer
Awk version:
Code:
awk 'NR>y+x{print A[NR%y]} {A[NR%y]=$0}' x=5 y=3 file

Last few lines of cfajohnson's post present a similar awk solution, right?
# 6  
Quote:
Originally Posted by elixir_sinari
Last few lines of cfajohnson's post present a similar awk solution, right?
Yes, we crossposted, I acknowledged that..

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

Test Your Knowledge in Computers #201
Difficulty: Easy
Perl is implemented as a core interpreter, written in C, together with a large collection of modules, written in Perl and C.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. 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

3. Shell Programming and Scripting

Print n lines from top and n lines from bottom of all files with .log extenstion

Oracle Linux 6.4 In a directory I have more than 300 files with the extension .log I want the first 5 and last 5 lines of these .log files to be printed on screen with each file's name. Expected output : Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log !! Authentication... (7 Replies)
Discussion started by: kraljic
7 Replies

4. Shell Programming and Scripting

Transpose Data form Different form

HI Guys, I have data in File A.txt RL03 RL03_A_1 RL03_B_1 RL03_C_1 RL03 -119.8 -119.5 -119.5 RL07 RL07_A_1 RL07_B_1 RL07_C_1 RL07 -119.3 -119.5 -119.5 RL15 RL15_A_1 RL15_C_1 RL15 -120.5 -119.4 RL16... (2 Replies)
Discussion started by: asavaliya
2 Replies

5. Shell Programming and Scripting

delete lines form file

Hi i am writing a cron job. in this script i need to delete some line which is match with some pattern. following code i written for deletion sed '1,'$Max_LIneNo' d' myfile.txt >tempfile.tmp mv tempfile.tmp myfile.txt this command is working fine but the problem is that after this... (5 Replies)
Discussion started by: Himanshu_soni
5 Replies

6. 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

7. Shell Programming and Scripting

How to extract visually blank lines form the file

Hi, Could some one help me to get rid of visually blank lines from a file using shell or awk or sed (on Solaris machine)? When I use grep grep -v ^$ inputfile >outputfile it removes some blank lines.. but it seems some tab plus space balnk lines remains. thaen I used "grep -v '^]*$' ... (1 Reply)
Discussion started by: hadsuresh
1 Replies

8. Shell Programming and Scripting

Random lines selection form a file.

>cat data.dat 0001 Robbert 0002 Nick 0003 Mark ....... 1000 Jarek (3 Replies)
Discussion started by: McLan
3 Replies

9. UNIX for Advanced & Expert Users

Changing Unix form to Microsoft Word form to be able to email it to someone.

Please someone I need information on how to change a Unix form/document into a microsoft word document in order to be emailed to another company. Please help ASAP. Thankyou :confused: (8 Replies)
Discussion started by: Cheraunm
8 Replies

Featured Tech Videos