Re: Deleting a line from a file
On Thu, 15 May 2014 09:55:52 +0200, Bernd Petrovitsch said: > 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). Actually, you want to use /^\/opt\/new1[ \t]/d because otherwise it will also delete lines that contain /usr/opt/new1 and /opt/new12. So you want a pattern that matches ^ anchor at start, the actual /opt/new1 pathname, and then either a blank or tab whitespace pgptHt0An8Adt.pgp Description: PGP signature ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
On Mit, 2014-05-14 at 11:18 -0400, valdis.kletni...@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 : be...@petrovitsch.priv.at LUGA : http://www.luga.at ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
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 : be...@petrovitsch.priv.at LUGA : http://www.luga.at ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
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. pgpvij8kG0aVH.pgp Description: PGP signature ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
Please find response inline. On Wed, May 14, 2014 at 9:44 PM, 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 ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
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) pgpgfLmgh_JZL.pgp Description: PGP signature ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
Please find my response inline- On Wed, May 14, 2014 at 8:04 PM, Bernd Petrovitsch 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 ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
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) pgpAcbxZ3MSYc.pgp Description: PGP signature ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Re: Deleting a line from a file
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 : be...@petrovitsch.priv.at LUGA : http://www.luga.at ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Deleting a line from a file
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 ___ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies