deleting a particular line in a file

Go down

deleting a particular line in a file

Post by akalya on Wed Mar 17, 2010 4:13 pm

how do i delete the nth line in a file??

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: deleting a particular line in a file

Post by Christopher on Wed Mar 17, 2010 5:43 pm

we r working with sequential files, it s not possible to delete a line directly.
If u want to delete a particular line,
1)search for n newline character and move the cursor to begin of that particular line and store the position in pos1.
2)search for next newline character and store the position in pos2.
3)read from position 'pos2' to end of file and store it in a buffer.
4)Now move the cursor to position 'pos1'
5)write the buffer back into the file and manually insert a EOF at the end.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: deleting a particular line in a file

Post by akalya on Wed Mar 17, 2010 6:51 pm

i want it in shell scripting....look into exercise 2 in lab session 2

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: deleting a particular line in a file

Post by mightyganesh on Wed Mar 17, 2010 7:04 pm

hey we tried like this

[335920@oracleclient Srinath]$ cat > gan.txt
this is line 1
line 2
line 3
line 4
line 5
line 6
[335920@oracleclient Srinath]$ num_line=`wc -l gan.txt | cut -d" " -f1`;(head -`expr 3 - 1` gan.txt ; tail -`expr $num_line - 3` gan.txt) > temp ; cp temp gan.txt ; rm temp
[335920@oracleclient Srinath]$ cat gan.txt
this is line 1
line 2
line 4
line 5
line 6


so if u wanna remove n-th line then replace 3 by n in the statement.

if u ppl wanna shell script to delete n th line
try this

echo " enter the line to be deleted"
read line_no
num_line=`wc -l gan.txt | cut -d" " -f1`
(head -`expr $line_no - 1` gan.txt ; tail -`expr $num_line - $line_no` gan.txt) > temp
cp temp gan.txt
rm temp
avatar
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 30

View user profile http://ganeshguna.blogspot.com

Back to top Go down

Re: deleting a particular line in a file

Post by scorpionKING on Thu Mar 18, 2010 2:31 pm

sed can be used to delete line/s in a file:

Code:
sed 3d <filename>
will delete the third line in the file.

Code:
sed 3,5d <filename>
will delete lines from 3 to 5

To customize the deletion of a line by getting user input use the following:
Code:
echo -n "Enter the line number: "
read lineNum
sed `expr $lineNum`d <filename>

Keep Bashing........
avatar
scorpionKING

Posts : 1
Points : 1
Join date : 2010-03-02
Age : 31

View user profile

Back to top Go down

Re: deleting a particular line in a file

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum