How to delete line number 3,6,9,12,15 and so on?


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
How to delete line number 3,6,9,12,15 and so on?

Hello,

How can I remove line number 3,6,9,12,15,18 and so on for a text file using sed? The line number pattern is multiple 3.

Thanks in advance.
# 2  
Code:
awk 'NR%3' file

These 2 Users Gave Thanks to Yoda For This Post:
# 3  
You mean the line or the line numbers?
# 4  
Code:
sed 'n;n;d' file

GNU sed:
Code:
sed 0~3d file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Quote:
Originally Posted by ahamed101
You mean the line or the line numbers?
Dear ahamed101,

I mean 3rd, 6th, 9th, 12th, 15th line and so on. Thanks. It is solved.

Last edited by huiyee1; 03-12-2014 at 04:10 AM.. Reason: typo mistake
# 6  
Code:
awk ' NR/3 ~ /\./ ' file

This User Gave Thanks to anbu23 For This Post:
# 7  
@anbu: Novel approach, to force the number into string context. It brings with it, however, that it is implementation dependent whether locale is used to convert it and whether a "." is part of the string..

In BSD awk, for example it will not work with the wrong locale:
Code:
$ printf "%s\n" {1..17} | LC_NUMERIC=nl_NL awk ' NR/3 ~ /\./ '
$

Code:
$ printf "%s\n" {1..17} | LC_NUMERIC=en_US awk ' NR/3 ~ /\./ '
1
2
4
5
7
8
10
11
13
14
16
17


Last edited by Scrutinizer; 03-12-2014 at 04:17 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register for Dates, Times and to Reply

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

Test Your Knowledge in Science: Gadgets
Difficulty: Easy
Microphones can be used not only to pick up sound, but also to project sound similar to a speaker.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete a specific line containing non consecutive number?

Dear Specialists, I have following data 1 1 2 2 2 3 3 3 6 4 3 4 5 4 9 6 5 11 7 6 7 and I would like to obtain data like below 1 1 2 2 2 3 4 3 4 7 6 7 (2 Replies)
Discussion started by: Ryan Kim
2 Replies

2. UNIX for Dummies Questions & Answers

How to delete lines above a certin line number in bash shell

Hi, I have written a script that returns the line number of the pattern i want and i stored the line number in a variable(getlinenumber).Now i want to delete all the lines in a file above this line number which is stored in a variable. i am using sed '1,$getlinenumberd' > file1.txt which is... (2 Replies)
Discussion started by: learninguser235
2 Replies

3. Shell Programming and Scripting

How to delete several lines from file by line number?

Hi I am using the following command to delete a line from the file by line number: line_number=14 sed "${line_number}d" inputfilename > newfilename Is there a way to modify this command to specify the range of lines to be deleted, lets say from line 14 till line 5 ? I tried using the... (5 Replies)
Discussion started by: aoussenko
5 Replies

4. Shell Programming and Scripting

how to delete the line by line number

Hi guys, is there a way to delete a line from a text file by providing a line number for the line I want to remove? I work in ksh88... Thanks a lot (3 Replies)
Discussion started by: aoussenko
3 Replies

5. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

6. Shell Programming and Scripting

delete multiple lines by line number

I have file with 10000 records and i need to delete the lines in single shot based on line number range say from 10 to 51 , 53 to 59 , 105 to 107, 311 to 592 etc... between range works fine for me but how to achive for above case? please help sed '10,51 {d}' infile > outfile (5 Replies)
Discussion started by: zooby
5 Replies

7. Shell Programming and Scripting

How would i delete a line at specific line number

Hi guys , I m writing a script to delete a line at particular location. But i m unable to use variable for specifying line number. for example. sed -n '7!p' filename works fine and deletes 7th line from my file but sed -n '$variable!p' filename gives following error. sed: -e... (12 Replies)
Discussion started by: pinga123
12 Replies

8. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

9. UNIX for Dummies Questions & Answers

delete multiple lines by line number

I have been googling, but cannot find that works for me. I have a text file tmp.out with contents: sadfsdf sdfosuidhfousdhof soduhf osdfu osudfhosudhfd sdfgsdfg asdfiojhsdf asdoludhflsdjfhskldjfhsdjdlfsjdhnlj h sdja ouahsdjdafkljsa oljhljh I have another file... (11 Replies)
Discussion started by: ChicagoBlues
11 Replies

10. UNIX for Advanced & Expert Users

delete records using line number(NR)

Hai I have a flat file which contains more than 6 crore lines or records. I want to delete only one line, using line number. For example I want to delete 414556 th line . How to do this using sed or awk command. thanks (3 Replies)
Discussion started by: tkbharani
3 Replies

Featured Tech Videos