Deleting specific lines from text file via scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting specific lines from text file via scripting
# 1  
Old 04-03-2014
Linux Deleting specific lines from text file via scripting

Hi,

I'm trying to search for some number and from that line, i need to delete the 5th line exactly.

Eg:
Consider below as text file data:

10000
a
b
c
d
e
.
.
.
10000
w
q
t
y
s

in the above i need to delete 'e','s' and do the same for all matched numbers and update the file.

What i tried is using sed i'm able to find the line number for that matched number but i'm stuck in moving to line no. 5 from 1st matched record.

Can someone kindly suggest some way to do achieve this.
# 2  
Old 04-03-2014
Code:
awk '$0 ~ patt {n = NR} NR != n+5' patt='10000' file

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 04-03-2014
Thanks. it works cool.

isn't that possible with sed ?
# 4  
Old 04-03-2014
Code:
sed '/10000/{n;n;n;n;n;d}' file

# 5  
Old 04-11-2014
I found a flaw in that 'awk' code..

lets say file as c.txt :
Code:
10000
a
b
c
d
e
..
10000
a
b
c
d
e
..
.

It works fine for the above file as the first set of data is that of same pattern we provide in that code..

Now lets say as d.txt :
Code:
20000
a
b
c
d
e
.
.
10000
a
b
c
d
e
.
.

It removes the 'e' from first set of lines also..

Hope you getting the issue.

---------- Post updated 04-11-14 at 09:05 AM ---------- Previous update was 04-10-14 at 10:44 AM ----------

Can someone help me in this ?

---------- Post updated at 09:06 AM ---------- Previous update was at 09:05 AM ----------

Can someone help me out for this issue ?
# 6  
Old 04-12-2014
Try
Code:
awk '$0 ~ PATT {P=NR+5} NR!=P' PATT=10000 file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 04-17-2014
I was trying to do this to all files ending with .txt in my destination directory using the following code :
Code:
cd /my_destination_dir/

for i in `ls *.txt`
do
awk '$0 ~ pat {n=NR+5} NR!=n' pat=10000 $i > $i
done

the given awk code works well for a single file even made with run time arguments but to make this dynamic for all files its not getting achieved.

What my code does is - making all .txt files to empty.

Can you please tell where i'm going wrong in the code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue deleting all lines (having a specific string) in a file

I'm trying to create a script. There are 2 files - fileA.log & fileB.log fileA.log has the below data : aaaa cccc eeee fileB.log has the below data : cjahdskjah aaaa xyz jhaskjdhas bbbb abc ajdhjkh cccc abc cjahdskjah ... (7 Replies)
Discussion started by: Pandee
7 Replies

2. Shell Programming and Scripting

Help required deleting specific lines from file

Hi, I have a file with 20 columns of data and hundreds of lines of the same format. Here is an example line. The data repeats underneath with the same format. 15 1 4 GLY - 1 65 LYSH 23 N - 24 H - 634 O 0.188 157.552 487 48.70I have been sorting this data by hand but I was wondering if I... (3 Replies)
Discussion started by: livbaddeley
3 Replies

3. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

4. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file filled with dates, such as: 04-08-2011 message 04-08-2011 message 03-08-2011 message 01-08-2011 message 31-07-2011 message 24-07-2011 message 15-07-2011 message 13-12-2008 message 26-11-2007 message And I want to delete those lines whose date is older than 10... (5 Replies)
Discussion started by: asanchez
5 Replies

5. UNIX for Dummies Questions & Answers

Deleting specific rows from a text file

How do I go about deleting specific rows from a text file (given row number)? (5 Replies)
Discussion started by: evelibertine
5 Replies

6. Shell Programming and Scripting

deleting specific lines in a file

Hello, I have a file like: 26-07-2011 sunz02 message1 26-07-2011 sunz02 message2 26-07-2011 sunz02 message3 15-07-2011 sunz02 message1 15-07-2011 sunz02 message2 15-07-2011 sunz02 message3... (5 Replies)
Discussion started by: asanchez
5 Replies

7. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file like this one: 03-07-2011 sunz02 message1 03-07-2011 sunz02 message2 03-07-2011 sunz02 message3 01-07-2011 sunz02 message1 01-07-2011 sunz02 message2 01-07-2011 sunz02 ... (1 Reply)
Discussion started by: asanchez
1 Replies

8. UNIX for Dummies Questions & Answers

Help with deleting specific rows from a text file

I know this is a complicated question but I will try to illustrate it with some data. I have a data file that looks like the following: 1341 NA06985 0 0 2 46.6432798439 1341 NA06991 NA06993 NA06985 2 48.8478948517 1341 NA06993 0 0 1 45.8022601455 1340 NA06994 0 0 1 48.780669145 1340... (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

deleting specific lines in a file

I want to delete all lines from a file (orig_file) that contain the regex values (bad_inv_list) I tried a for each loop with sed but it isn't working for file in `cat bad_inv_list`; do sed '/$file/d' orig_file > pared_down_file.1 mv pared_down_file.1 orig_file done I've added... (2 Replies)
Discussion started by: verge
2 Replies

10. Shell Programming and Scripting

Deleting specific lines in a file

I have a file which has some lines starting with a particular word. I would like to delete 5 lines before each such line containing that particular word. eg: line1 line2 line3 line4 line5 line6 "particular word"... I would like to delete line2-line6 and all such occurences in that... (4 Replies)
Discussion started by: ramu_1980
4 Replies
Login or Register to Ask a Question