how to delete nth line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to delete nth line
# 1  
Old 03-19-2012
how to delete nth line

can you please tell me

(1) how to delete 1st and 3rd line only from a file.
(2) How to delete last 4 lines in a file that has 2 blank lines out of last 4 lines.

Thank you.
# 2  
Old 03-19-2012
Code:
# Exclude lines 1 and 3
awk '!((NR==1)||(NR==3))' < inputfile > outputfile

Code:
# Exclude last 4 lines
head -n -4 < inputfile > outputfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-19-2012
Code:
sed '1d;3d' inputfile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete after nth occurence of string in each line

Hello, Environment: I am under Ubuntu 18.04 bionic. I have an sql file consisting of 10K lines. Objective: What I am trying to attain is to remove everything coming after 2nd tab in each line. While searching for the answer, I found two answers and both gave expected result just for the first... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

4. Shell Programming and Scripting

Delete until Nth occurence (sed, awk)

Hello people, Once more I need your help with SED/AWK I need to delete up to the Nth occurence of a char (from the beggining) and until the Mth occurence of a char (from the end) Example: Input: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z Output: i,j Must delete up to the... (2 Replies)
Discussion started by: drbiloukos
2 Replies

5. UNIX for Dummies Questions & Answers

Give name for every nth line

Hi all, Greetings. I am facing some troubles and hope that the professionals here could help me. I am handling a large dataset, and part of it is like below: 1 ab139 0 752566 G A 1 ab151 0 846808 T C 1 ab142 0 854250 G A 1 ab061 0 861808 A G 1 ab043 0 873558 T G... (4 Replies)
Discussion started by: Amanda Low
4 Replies

6. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

7. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

8. Shell Programming and Scripting

Using tr, sed or awk to delete text from nth column only

Hi everyone, this is my first post here, I hope someone can help me. I have a file which I need to delete characters '_F3' from the end of the text in the first column. The problem is that the characters may also occur elsewhere in the file (i.e. second columns onwards). I tried sed (thinking I... (6 Replies)
Discussion started by: hlwright
6 Replies

9. Shell Programming and Scripting

delete line upto the nth occurence of a particular charachter.

hi all i want to delete a line upto a particular character. here is example. cp cms/images/wifi-zone.png i want to delete the line till . (cp cms/images/wifi-zone.) so the output wud be "png" only how can i do it? also please note down that dot (.) can also occur multiple... (12 Replies)
Discussion started by: kashifv
12 Replies

10. Shell Programming and Scripting

Get Nth record from last line

I have a growing file . I knew only last few lines which is constant for evey job run. I'd need to pull the Nth record from the last line. In this case i should not use search pattern. (2 Replies)
Discussion started by: ford2020
2 Replies
Login or Register to Ask a Question