Skip first and last n records with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Skip first and last n records with awk
# 1  
Old 01-03-2011
Skip first and last n records with awk

Hi,
I have an awk code that reads an input file, checks the 4th column and tells if its fine.
Code:
#!/bin/ksh
{ if ($4 == 0)
print "fine"
else
print "some problem" }' FILENAME

My problem is that, I dont want to check the first 3 and last 3 lines.
This can be hard coded by using BEGIN and END in awk code.
But, I dont want to hardcode.
Could you please suggest, if I can go with FOR loop or something. if yes,how?

Last edited by radoulov; 01-03-2011 at 09:19 AM..
# 2  
Old 01-03-2011
Hello,

Per our forum rules, all threads must have a descriptive subject text. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". Post subjects like "Execution Problems with Cron" or "Help with Backup Shell Script".

The reason for this is that nearly 95% of all visitors to this site come here because they are referred by a search engine. In order for future searches on your post (with answers) to work well, the subject field must be something useful and related to the problem!

In addition, current forum users who are kind enough to answer questions should be able to understand the essence of your query at first glance.

So, as a benefit and courtesy to current and future knowledge seekers, please be careful with your subject text. You might receive a forum infraction if you don't pay attention to this.

Thank you.

The UNIX and Linux Forums

---------- Post updated at 01:18 PM ---------- Previous update was at 01:15 PM ----------

Code:
awk 'NR > 3 && nr - 3 > NR {
  print $4 == 0 ? "fine" : "some problem"
  }' nr="$(wc -l < infile)" infile

# 3  
Old 01-03-2011
TIMTOWTDI:
Code:
head -n -3 inputfile | tail -n +3 | awk '....'

# 4  
Old 01-03-2011
Not sure what is the syntax error over here.
its saying
Code:
 syntax error The source line is 2.
 The error context is
                 >>> while <<<  (NR > 3) {
 awk: Quitting
 The source line is 2.

my code is
Code:
FILE=$1
awk '
while (NR > 3) {
 if ($4 == 0)
 print "fine"
 else
 print "some problem" } ' $FILE

tried a number of times..with multiple trail n error. But in vein. PLz help
# 5  
Old 01-03-2011
Just read and follow the suggestions you received ...
# 6  
Old 01-03-2011
Hi Radoulov,
thanks for responding.
Thing is that, The piece of codfe that you have provided produced syntax error,even though I could understand the logic behind your code.
After that, I tried to resolve it in many ways and tried with a while condition.
m posting my query again because, I could not get exactly from the error message, where i am going wrong.
else no one wud play and pass one's time with silly ping pongs.
# 7  
Old 01-03-2011
OK,
just copy/paste here the command you execute and the exact error message
(I mean the code from the previous posts, not the one you modified)
so we don't have to guess.

In any case the standard suggestion applies: use nawk or /usr/xpg4/bin/awk if you're on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to skip header row and add string to field

The awk below does put in VUS in the 9th field but I can not seem to skip the header then add the VUS. I tried to incorporate NR >=2 and NR > 1 with no luck. Thank you :). input Chr Start End Ref Alt Func.refGene PopFreqMax CLINSIG Classification chr1 43395635 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

awk - Skip x Number of Lines in Counter

Hello, I am new to AWK and in UNIX in general. I am hoping you can help me out here. Here is my data: root@ubuntu:~# cat circuits.list WORD1 AA BB CC DD Active ISP1 ISP NAME1 XX-XXXXXX1 WORD1 AA BB CC (9 Replies)
Discussion started by: tattoostreet
9 Replies

4. UNIX for Dummies Questions & Answers

Using awk to skip record in file

I need to amend the code blow such that it reads a "black list" before the "print" statement; if "substr($1,1,6)" is found in the "blacklist" it will ignore that record and continue. the code is from an awk script that is being called from shell script which passes the input values. BEGIN { "date... (5 Replies)
Discussion started by: bazel
5 Replies

5. Shell Programming and Scripting

Split a large file in n records and skip a particular record

Hello All, I have a large file, more than 50,000 lines, and I want to split it in even 5000 records. Which I can do using sed '1d;$d;' <filename> | awk 'NR%5000==1{x="F"++i;}{print > x}'Now I need to add one more condition that is not to break the file at 5000th record if the 5000th record... (20 Replies)
Discussion started by: ibmtech
20 Replies

6. Shell Programming and Scripting

want to skip a line in XML file using awk

HI All, I am trying to split a xml using awk. now the issue is i want to skip three lines from the xml file. first two and last one based on pattern. plz some one help. i am new to awk and struggling :wall: <?xml version="1.0"?> <notification> ..... ..... ..... ..... ........ (24 Replies)
Discussion started by: ganesan kulasek
24 Replies

7. Shell Programming and Scripting

awk: skip x lines and ssh

Im trying to ssh to a remote machine to grep 'x info' *.log and Im able to get the grep output as expected but "after" the policies (1st 14 lines) - I need to skip the first 14 lines. Its SunOS. Plz help??? (7 Replies)
Discussion started by: anthonyraj75
7 Replies

8. Shell Programming and Scripting

awk - skip x lines and ssh

Im trying to ssh to a remote machine to grep 'x info' *.log and Im able to get the grep output as expected but "after" the policies (1st 14 lines) - I need to skip the first 14 lines. Its SunOS. Plz help??? (1 Reply)
Discussion started by: anthonyraj75
1 Replies

9. Shell Programming and Scripting

AWK to skip comments in XML file

Hello, I'm trying to make a shell script to skip comments from an XML file, but with the code below only deletes comments that are in one line. Can you tell me what can be added here? nawk ' { if($0 !~/<!--/) { a=0 } if($0 ~/<!--/ && $0 ~/-->/) {a=1} if($0 ~/<!--/) {a=1} if... (1 Reply)
Discussion started by: majormark
1 Replies

10. Shell Programming and Scripting

Skip parsing the header record - Awk

Guys.... Got a scenario in which I need to skip parsing the header record while I do an awk. Does awk has the flexibility to accomplish this?. If so, how do we do this?. Thanks !!! -Anduzzi :) (2 Replies)
Discussion started by: anduzzi
2 Replies
Login or Register to Ask a Question