Deleting line with range check in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting line with range check in awk
# 1  
Old 06-15-2014
Linux Deleting line with range check in awk

Consider a input.txt as:
Code:
country=india
ram
ajit
sham
vijay                //need to be removed
country=india
suman
tharan
raju

what i did is:
Code:
awk '$0~pat {n=NR+4} NR!=n' pat='country=india' input.txt > output.txt

My looking to the above code is between any two country=india , number of lines should be 3 NR == 3

I'm not able do this, help me with this.

Last edited by Gautham; 06-15-2014 at 05:26 AM.. Reason: updated the requirement
# 2  
Old 06-15-2014
Try :
Code:
$ awk 'FNR != s; $0 ~ pat{s = FNR + 4}' pat='country=india' file

Quote:
Originally Posted by Gautham
Consider a input.txt as:
Code:
country=india
ram
ajit
sham
vijay                //need to be removed
country=india
suman
tharan
raju

what i did is:
Code:
awk '$0~pat {n=NR+4} NR!=n' pat='country=india' input.txt > output.txt

I wanted to add a check condition to the above code like:
Code:
/country=india/ && /country=india/{ NR == 4 }

I'm not able do this, help me with this.

Last edited by Akshay Hegde; 06-15-2014 at 05:14 AM..
# 3  
Old 06-15-2014
Quote:
Originally Posted by Akshay Hegde
Try :
Code:
$ awk 'FNR != s; $0 ~ pat{s = FNR + 4}' pat='country=india' file

input:
Code:
country=india
ram
ajit
sham
vijay                          //need to be removed
country=india
suman
tharan
raju
country=india
suman
tharan
raju

Above code's output:
Code:
country=india
ram
ajit
sham
country=india
suman
tharan
raju
suman
tharan
raju

# 4  
Old 06-15-2014
Quote:
Originally Posted by Gautham
input:
Code:
country=india
ram
ajit
sham
vijay                          //need to be removed
country=india
suman
tharan
raju
country=india
suman
tharan
raju

Above code's output:
Code:
country=india
ram
ajit
sham
country=india
suman
tharan
raju
suman
tharan
raju

So what do you expect from your new input ?
# 5  
Old 06-15-2014
Quote:
Originally Posted by Akshay Hegde
So what do you expect from your new input ?

As you can see there is a line missing in #2 output. 'country=india'

My looking is actually between any two 'country=india' , NR = = 3
# 6  
Old 06-15-2014
Quote:
Originally Posted by Gautham


As you can see there is a line missing in #2 output. 'country=india'

My looking is actually between any two 'country=india' , NR = = 3
With your original input I get this

Code:
$ cat file
country=india
ram
ajit
sham
vijay                //need to be removed
country=india
suman
tharan
raju

$ awk 'FNR != s ;$0 ~ pat{s = FNR + 4}' pat='country=india' file
country=india
ram
ajit
sham
country=india
suman
tharan
raju

Please give proper description along with expected output.

---------- Post updated at 02:57 PM ---------- Previous update was at 02:52 PM ----------

Try this with your new input :
Code:
$ awk 'p,p{if($0 ~ p){print;if(k>0){k=0}next}else{if(++k !=4)print}}' p='country=india' file

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 06-15-2014
Quote:
Originally Posted by Akshay Hegde
With your original input I get this

Code:
$ cat file
country=india
ram
ajit
sham
vijay                //need to be removed
country=india
suman
tharan
raju

$ awk 'FNR != s ;$0 ~ pat{s = FNR + 4}' pat='country=india' file
country=india
ram
ajit
sham
country=india
suman
tharan
raju

Please give proper description along with expected output.

---------- Post updated at 02:57 PM ---------- Previous update was at 02:52 PM ----------

Try this with your new input :
Code:
$ awk 'p,p{if($0 ~ p){print;if(k>0){k=0}next}else{if(++k !=4)print}}' p='country=india' file

Yes. Its working fine now. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Check unused ports in a given range and assign an open one

Hi. I need to add code to my KSH script to automatically assign an open port number from a pre-defined range to an Oracle listener. Should I use: lsof -i or netstat -vatn or something else? Thanks. (9 Replies)
Discussion started by: user052009
9 Replies

3. Shell Programming and Scripting

awk - check time stamp between range or not

I want to check given time stamp is between the given time stamp or not. I am using AIX. YYYYMMDDHHMMSS abc.csv START TIME, END TIME 20130209018000,20130509022000 20120209018000,20130509022000 20120209018000,20130509022000 Script will check given time stamp is between above two range or... (2 Replies)
Discussion started by: vegasluxor
2 Replies

4. Shell Programming and Scripting

Deleting double quoted string from a line when line number is variable

I need to remove double quoted strings from specific lines in a file. The specific line numbers are a variable. For example, line 5 of the file contains A B C "string" I want to remove "string". The following sed command works: sed '5 s/\"*\"//' $file If there are multiple... (2 Replies)
Discussion started by: rennatsb
2 Replies

5. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

6. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

7. Shell Programming and Scripting

How to print range of a line?

Hi , I have a file content like following. In few lines I have fields with '"' and in few not. "Country" character varying(150), "Region" character varying(60), "Total Page Requests" numeric, "Pages Served" numeric, "Ad request" numeric, Ads Served ... (5 Replies)
Discussion started by: sussus2326
5 Replies

8. Shell Programming and Scripting

Deleting a line from a file with sed and awk?

cat file.txt fvnuiehuewf ruevhxncvkjrh zxjvurhfuwe jkhvBEGINvfnvf ijrgioe Trying to delete a line that has the pattern "BEGIN" cat sedtest filename=file.txt pattern=BEGIN sed "/^$pattern/d" "$filename" (9 Replies)
Discussion started by: cola
9 Replies

9. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies

10. UNIX for Dummies Questions & Answers

What/How to check before deleting files and directories

Hi there, I have a some directories containing web files that are old, and I need to remove them. I know that there might be sym links and hyperlinks pointing to these old directories. If that's the case, then I'll have to fix the links before deleting these old directories to avoid broken... (4 Replies)
Discussion started by: yvochan
4 Replies
Login or Register to Ask a Question