deleting a particular line in a file
4 posters
deleting a particular line in a file
how do i delete the nth line in a file??
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: deleting a particular line in a file
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.
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.
Re: deleting a particular line in a file
i want it in shell scripting....look into exercise 2 in lab session 2
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: deleting a particular line in a file
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
[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
Re: deleting a particular line in a file
sed can be used to delete line/s in a file:
To customize the deletion of a line by getting user input use the following:
Keep Bashing........
- Code:
sed 3d <filename>
- Code:
sed 3,5d <filename>
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........
scorpionKING- Posts : 1
Points : 1
Join date : 2010-03-02
Age : 36
Similar topics
» listing and deleting directories
» deleting non-empty directories??
» read from a file
» exact file search?
» Change Owner of a File
» deleting non-empty directories??
» read from a file
» exact file search?
» Change Owner of a File
Permissions in this forum:
You cannot reply to topics in this forum