deleting lines by number


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers deleting lines by number
# 1  
Old 02-12-2004
deleting lines by number

Is it possible to delete lines by their number? Also, I'd like to delete the last 3 rows of a file too. So from the front and back. Thanks.
# 2  
Old 02-12-2004
use perl.

read the file into an array and push undef into the line you want to delete.

to ditch the last 3 lines use the variable $#array_name (this is a numerical value of the total # of elements in the array) and use pop to get them off the stack.
# 3  
Old 02-12-2004
I haven't used perl before....but I need to get this done today. Is there another way?
# 4  
Old 02-12-2004
I just actually need to delete the first two and last three rows. Any solutions?
# 5  
Old 02-12-2004
I was messing around with this quite a bit.. I think something more efficient could be made in awk, but this'll do for now: Smilie
Code:
count=0
lines=`wc -l < "$3"`
((footer=lines-$2))

for LINE in `cat "$3"`; do
  ((count+=1))
  if [[ $count -gt $1 && $count -le $footer ]]; then
    echo "$LINE" >> tmpfile
  fi
done

mv tmpfile "$3"

If you want delete two lines from the top of the file and three lines from the bottom of the file, type: script_name 2 3 yourfile

Last edited by oombera; 02-12-2004 at 03:17 PM..
# 6  
Old 02-12-2004
Here it is in awk:
Code:
awk '
BEGIN {
 b=2
 e=3
 i="'"\"`wc -l < yourfile`\""'"
 c=0
}

{
 c+=1
 if (c > b && c <= (i-e))
  print
}
' yourfile

# 7  
Old 02-12-2004
I tried your script, and i had to input the command:
script_name 1 4 yourfile for it to work on my file.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

3. Shell Programming and Scripting

Deleting Line number from different file

file 1 abcde,dfgdfg,sdfds sdfsd,sdfd,sdfg,sdfs dwfds,sdfgdfg,sdfsdf file2 1 3 i need to delete the line numbers file 1 based on file2 i have taken in loop file 1 and i cant able to delete that line number in file 1 Plz help (4 Replies)
Discussion started by: ragu.selvaraj
4 Replies

4. Shell Programming and Scripting

deleting lines in ex

Hello, im using ex to manipulate some text. Im trying to delete all the lines except those on which a certain regex can be found. the important part of the script: ex temp << 'HERE' g/regex/p HERE this command prints the lines I want to end up with, but it doesnt delete the others.... (2 Replies)
Discussion started by: drareeg
2 Replies

5. UNIX for Dummies Questions & Answers

deleting a row if a certain column is below a certain number

How can you delete a row if a certain column is bigger than a certain number? I have the following input: 20080709 20081222 95750 1 0 0.02 94.88 20080709 20081222 95750 2 0 0.89 94.88 20080709 20081222 9575 1 0 0 94.88 20080709 20081222 9575 2 0 0 94.88 20080709 20081222 9587.5 1 0 0... (6 Replies)
Discussion started by: Pep Puigvert
6 Replies

6. UNIX for Dummies Questions & Answers

deleting a row if a certain column is below a certain number

How can you delete a row if a certain column is bigger than a certain number? I have the following input: 20080709 20081222 95750 1 0 0.02 94.88 20080709 20081222 95750 2 0 0.89 94.88 20080709 20081222 9575 1 0 0 94.88 20080709 20081222 9575 2 0 0 94.88 20080709 20081222 9587.5 1 0 0... (1 Reply)
Discussion started by: Pep Puigvert
1 Replies

7. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

8. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

9. Shell Programming and Scripting

Deleting particular lines.

hi all, i have got a scenario in which i need to delete all the lines that ends with file names. e.g. input can be cms/images/services_icons/callback.png cms/cms/images/services_icons/sync.php cms/cms/images/services_icons and output should be cms/cms/images/services_icons ... (13 Replies)
Discussion started by: kashifv
13 Replies

10. Shell Programming and Scripting

deleting lines

I am trying deleting lines from a text file using sed.. sed '/OBJECT="ABC/{N;N;N;d; }' will do if i have to delete lines starting with Object and next 3 lines but I was looking for a way to delet lines starting with OBJECT and all the lines till it reaches a blank lines ..or it reaches a... (8 Replies)
Discussion started by: ajnabi
8 Replies
Login or Register to Ask a Question