* Deleting a line from a file
@ 2014-05-14 14:28 Saket Sinha
2014-05-14 14:34 ` Bernd Petrovitsch
0 siblings, 1 reply; 10+ messages in thread
From: Saket Sinha @ 2014-05-14 14:28 UTC (permalink / raw)
To: kernelnewbies
Hi,
I have a file that has entries for different absolute path on
separate lines. eg:
/opt/new1
/opt/new2
I need to delete an entry from this file for a given path, for which I
am using sed.
sed -i 's#^/opt/new1.*$##g' file_entries.txt
However this is leaving blank line in between, which I don't want.
Can someone help me to do this without leaving blank lines in between.
Regards,
Saket Sinha
^ permalink raw reply [flat|nested] 10+ messages in thread* Deleting a line from a file
2014-05-14 14:28 Deleting a line from a file Saket Sinha
@ 2014-05-14 14:34 ` Bernd Petrovitsch
2014-05-14 15:18 ` Valdis.Kletnieks at vt.edu
2014-05-14 15:57 ` Saket Sinha
0 siblings, 2 replies; 10+ messages in thread
From: Bernd Petrovitsch @ 2014-05-14 14:34 UTC (permalink / raw)
To: kernelnewbies
Hi!
The original mail is off-topic as it has nothing to do wotj the Linux
kernel development as such, but:
On Mit, 2014-05-14 at 19:58 +0530, Saket Sinha wrote:
[...]
> I have a file that has entries for different absolute path on
> separate lines. eg:
> /opt/new1
> /opt/new2
>
> I need to delete an entry from this file for a given path, for which I
> am using sed.
>
> sed -i 's#^/opt/new1.*$##g' file_entries.txt
>
> However this is leaving blank line in between, which I don't want.
>
> Can someone help me to do this without leaving blank lines in between.
sed -i 's#^/opt/new1.*$#d' file_entries.txt
should do it ....
Bernd
--
Bernd Petrovitsch Email : bernd at petrovitsch.priv.at
LUGA : http://www.luga.at
^ permalink raw reply [flat|nested] 10+ messages in thread* Deleting a line from a file
2014-05-14 14:34 ` Bernd Petrovitsch
@ 2014-05-14 15:18 ` Valdis.Kletnieks at vt.edu
2014-05-15 7:55 ` Bernd Petrovitsch
2014-05-14 15:57 ` Saket Sinha
1 sibling, 1 reply; 10+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2014-05-14 15:18 UTC (permalink / raw)
To: kernelnewbies
On Wed, 14 May 2014 16:34:20 +0200, Bernd Petrovitsch said:
> sed -i 's#^/opt/new1.*$#d' file_entries.txt
You don't even need the leading 's'. Just /pattern/d is sufficient.
(And you can even do stuff like /pat1/,/pat2/s/old/new/ which will
change 'old' to 'new', but only from a line that contains pat1 up to a
line that contains pat2, and *not* changing it before pat1 or after pat2....)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140514/38c244d1/attachment.bin
^ permalink raw reply [flat|nested] 10+ messages in thread
* Deleting a line from a file
2014-05-14 15:18 ` Valdis.Kletnieks at vt.edu
@ 2014-05-15 7:55 ` Bernd Petrovitsch
2014-05-15 8:06 ` Valdis.Kletnieks at vt.edu
0 siblings, 1 reply; 10+ messages in thread
From: Bernd Petrovitsch @ 2014-05-15 7:55 UTC (permalink / raw)
To: kernelnewbies
On Mit, 2014-05-14 at 11:18 -0400, Valdis.Kletnieks at vt.edu wrote:
> On Wed, 14 May 2014 16:34:20 +0200, Bernd Petrovitsch said:
>
> > sed -i 's#^/opt/new1.*$#d' file_entries.txt
>
> You don't even need the leading 's'. Just /pattern/d is sufficient.
Ooops, yes, thanks. So
sed -i '/\/opt\/new1/d' file_entries.txt
should do it.
Just for the match, we do not need, the tailing ".*$" (because it
matches always). But we need /../,
so just quote the "/" in the path.
The "d" tells "sed" to delete the current line.
Bernd
--
Bernd Petrovitsch Email : bernd at petrovitsch.priv.at
LUGA : http://www.luga.at
^ permalink raw reply [flat|nested] 10+ messages in thread
* Deleting a line from a file
2014-05-14 14:34 ` Bernd Petrovitsch
2014-05-14 15:18 ` Valdis.Kletnieks at vt.edu
@ 2014-05-14 15:57 ` Saket Sinha
2014-05-14 16:14 ` Valdis.Kletnieks at vt.edu
2014-05-15 7:53 ` Bernd Petrovitsch
1 sibling, 2 replies; 10+ messages in thread
From: Saket Sinha @ 2014-05-14 15:57 UTC (permalink / raw)
To: kernelnewbies
Please find my response inline-
On Wed, May 14, 2014 at 8:04 PM, Bernd Petrovitsch
<bernd@petrovitsch.priv.at> wrote:
> Hi!
>
> The original mail is off-topic as it has nothing to do wotj the Linux
> kernel development as such, but:
>
I wanted to isolate the problem hence did not give the full context.
Actually I have written a file-system utility which mounts my
filesystem on a specific path and update it in /etc/fstab. When I
umount it, I delete the path from the /etc/fstab.
I had earlier written a C function to do that but it is corrupting the fstab.
SO now I am trying to run the above sed command this way
system(sed -i 's#^/opt/new1.*$##g' /etc/fstab).
The C function that was currupting the filesystem is listed below -
int removeEntryFromFSTAB(const char * fullPath, const char * fileName)
{
FILE *tabFileOld = NULL;
FILE *tabFileNew = NULL;
struct mntent *m;
char newFileName[PATH_MAX];
int rc = -1;
tabFileOld = setmntent( fileName, "r" ); // Open for writing now
if (tabFileOld == NULL )
goto end;
tabFileNew = setmntent(newFileName, "w");
if (tabFileNew == NULL )
goto end;
while ((m = getmntent(tabFileOld)) != NULL)
{
if (tcscmp(MY_FS_TYPE, m->mnt_type) == 0)
{
if ((tcscmp(fullPath, m->mnt_dir) == 0))
continue;
}
if (addmntent(tabFileNew, m) != 0)
goto end;
}
endmntent(tabFileOld);
endmntent(tabFileNew);
tabFileNew = NULL;
tabFileOld = NULL;
rename(newFileName, fileName))
rc = 0;
end:
if (tabFileNew != NULL)
endmntent(tabFileNew);
if (tabFileOld != NULL)
endmntent(tabFileOld);
sync();
return rc;
}
Kindly let me know, if you think there is a better way.
Regards,
Saket Sinha
^ permalink raw reply [flat|nested] 10+ messages in thread* Deleting a line from a file
2014-05-14 15:57 ` Saket Sinha
@ 2014-05-14 16:14 ` Valdis.Kletnieks at vt.edu
2014-05-14 16:43 ` Saket Sinha
2014-05-15 7:53 ` Bernd Petrovitsch
1 sibling, 1 reply; 10+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2014-05-14 16:14 UTC (permalink / raw)
To: kernelnewbies
On Wed, 14 May 2014 21:27:06 +0530, Saket Sinha said:
> char newFileName[PATH_MAX];
> tabFileNew = setmntent(newFileName, "w");
And what is the new file name? You have random trash on the stack here.
(Note that this is C 101 - if you can't debug this on your own, you
probably shouldn't be messing with filesystem code until you have more
C experience)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140514/3f5d7e54/attachment.bin
^ permalink raw reply [flat|nested] 10+ messages in thread
* Deleting a line from a file
2014-05-14 16:14 ` Valdis.Kletnieks at vt.edu
@ 2014-05-14 16:43 ` Saket Sinha
2014-05-14 17:51 ` Valdis.Kletnieks at vt.edu
0 siblings, 1 reply; 10+ messages in thread
From: Saket Sinha @ 2014-05-14 16:43 UTC (permalink / raw)
To: kernelnewbies
Please find response inline.
On Wed, May 14, 2014 at 9:44 PM, <Valdis.Kletnieks@vt.edu> wrote:
> On Wed, 14 May 2014 21:27:06 +0530, Saket Sinha said:
>
>> char newFileName[PATH_MAX];
>
>> tabFileNew = setmntent(newFileName, "w");
>
> And what is the new file name? You have random trash on the stack here.
int removeEntryFromFSTAB(const char * fullPath, const char * fileName);
I am sending /etc/fstab in fileName to this function and the path to
be deleted in fullPath
NOW
char newFileName[PATH_MAX];
......
.......
rename(newFileName, fileName));
I am taking a new file, writing my entries to it and then replacing it
with original file. I don't find anything wrong with that
> (Note that this is C 101 - if you can't debug this on your own, you
> probably shouldn't be messing with filesystem code until you have more
> C experience)
>
Sorry but I haven't been able to debug that. I admit it.
Regards,
Saket Sinha
^ permalink raw reply [flat|nested] 10+ messages in thread
* Deleting a line from a file
2014-05-14 16:43 ` Saket Sinha
@ 2014-05-14 17:51 ` Valdis.Kletnieks at vt.edu
0 siblings, 0 replies; 10+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2014-05-14 17:51 UTC (permalink / raw)
To: kernelnewbies
On Wed, 14 May 2014 22:13:51 +0530, Saket Sinha said:
> I am sending /etc/fstab in fileName to this function and the path to
> be deleted in fullPath
OK.
> char newFileName[PATH_MAX];
This lives on your function call stack. As such, it contains whatever
was in that memory until you change it. And you never change it.
tabFileOld = setmntent( fileName, "r" ); // Open for writing now
The comment indicates there's some confusion going on. Not a good sign....
tabFileNew = setmntent(newFileName, "w");
if (tabFileNew == NULL )
goto end;
This probably doesn't do what you think it does. What value of 'newFileName'
is used to create the new file? For bonus points, what guarantees that
it points to an object that's on the same filesystem as your old filename?
(This will actually fail to throw an error as long as newFileName[0] isn't
a '\0'. But that's different from actually working properly....)
And you *really* want to get this issue well-understood before hacking kernel
code, as the kernel doesn't do very much error checking. You make this sort of
error in kernel code, you will crash the machine - if you're lucky. If you're
unlucky, the system will silently corrupt entire filesystems on you to the
point where fsck won't help. (Want *real* debugging fun? Get your system
to a state where /boot fsck's just fine, but something has silently overwritten
several data blocks in /boot/vmlinuz or /lib/ld-linux.so. *That* will keep
you busy for a while.. :)
if (tcscmp(MY_FS_TYPE, m->mnt_type) == 0)
You want to use strncmp() or similar here. And you want to learn why
you shouldn't be using tcscmp() (Hint: are either MY_FS_TYPE or m->mnt_type
allowed to be UTF-8 strings? :)
rename(newFileName, fileName))
rc = 0;
You probaby want to check the return code:
rc = rename(newFileName, fileName);
if (!rc) { perror() or something....
as you're *very* likely to get EXDEV as an error due to previous code.
You're also missing error checking on every single endmntent() call - what
happens if one of those fails (which *can* happen)?
sync();
You don't want to do that. You should have done an fsync() (except that's
technically wrong as you're mixing it with stdio and not setting it to
unbuffered) or a syncfs() instead.
return rc;
This fails to capture a number of potential errors.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140514/165c54b0/attachment.bin
^ permalink raw reply [flat|nested] 10+ messages in thread
* Deleting a line from a file
2014-05-14 15:57 ` Saket Sinha
2014-05-14 16:14 ` Valdis.Kletnieks at vt.edu
@ 2014-05-15 7:53 ` Bernd Petrovitsch
1 sibling, 0 replies; 10+ messages in thread
From: Bernd Petrovitsch @ 2014-05-15 7:53 UTC (permalink / raw)
To: kernelnewbies
On Mit, 2014-05-14 at 21:27 +0530, Saket Sinha wrote:
[...]
> I wanted to isolate the problem hence did not give the full context.
> Actually I have written a file-system utility which mounts my
> filesystem on a specific path and update it in /etc/fstab. When I
> umount it, I delete the path from the /etc/fstab.
I don't know if you want to do that - /etc/mtab is (historically?) used
to record actually mounted filesystems.
(At least) in the Linux world, there is /proc/mounts where the kernel
tells you exactly that.
Bernd
--
Bernd Petrovitsch Email : bernd at petrovitsch.priv.at
LUGA : http://www.luga.at
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2014-05-15 8:06 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-14 14:28 Deleting a line from a file Saket Sinha
2014-05-14 14:34 ` Bernd Petrovitsch
2014-05-14 15:18 ` Valdis.Kletnieks at vt.edu
2014-05-15 7:55 ` Bernd Petrovitsch
2014-05-15 8:06 ` Valdis.Kletnieks at vt.edu
2014-05-14 15:57 ` Saket Sinha
2014-05-14 16:14 ` Valdis.Kletnieks at vt.edu
2014-05-14 16:43 ` Saket Sinha
2014-05-14 17:51 ` Valdis.Kletnieks at vt.edu
2014-05-15 7:53 ` Bernd Petrovitsch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).