Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to read a file without opening the file and delete last line? Post 302897357 by alister on Sunday 13th of April 2014 03:01:15 PM
Old 04-13-2014
As bakunin stated, you cannot modify the contents of a file without opening it. The best you can do is open the file, find the penultimate newline character, and truncate to that positiion.

Regards,
Alister
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

delete file without opening vi

hi there guys, wonder if any gurus can help me out on this one... try searching the past threads but cant find anything. i have this huge file but when i use vi to open it it gives me the following error: <"pmrepserver.txt""/var/tmp/Ex86200" There is not enough space in the file... (7 Replies)
Discussion started by: lweegp
7 Replies

2. Shell Programming and Scripting

Urgent help required in deleting a line without opening a file usinga shell script

Hi, I need a help in deleting a line matching a particular pattern in a file using shell script without opening the file. The file is a .c/.cpp file. Is it possible? Thanks (6 Replies)
Discussion started by: naan
6 Replies

3. Shell Programming and Scripting

How to delete a particular text without opening the file.

Hi, Could someone tell me how to delete a particular text inside an existing file without opening the file? e.g. I have a text file called mytext.txt which contains three lines of text below and I want to delete “ ;” and delete the second line “b ;” including the carriage return. a ; b ; c ;... (12 Replies)
Discussion started by: stevefox
12 Replies

4. UNIX for Dummies Questions & Answers

Urgent help needed to delete some text without opening the file in unix

Hi To delete some text in 2 files in line1 ( not complete line) in unix without opening the files. For example: source file is like this <?xml version="1.0"... (5 Replies)
Discussion started by: pyaranoid
5 Replies

5. Shell Programming and Scripting

How to Delete string without opening a file

Hi Experts, I have several big size file arround 900 MB. From the file I need to delete some common strings but without opening the file. here is example- in file <?xml version='1.0' encoding='ISO-8859-1' standalone='no'?> <LogItems> <log logid="8423b5ae190810252359350480/1/1/1"> ... (6 Replies)
Discussion started by: thepurple
6 Replies

6. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

7. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

8. Shell Programming and Scripting

echo ls to a file and then read file and selectively delete

I'm trying to write a script that will do an ls of a location, echo it into a file, and then read that file and selectively delete files/folders, so it would go something like this: cd $CLEAN_LOCN ls >>$TMP_FILE while read LINE do if LINE = $DONTDELETE skip elseif LINE =... (2 Replies)
Discussion started by: MaureenT
2 Replies

9. Shell Programming and Scripting

Read file line by line and process the line to generate another file

Hi, i have file which contains data as below(Only sample shown, it may contain more data similar to the one shown here) i need to read this file line by line and generate an output file like the one below i.e based on N value the number of MSISDNs will vary, if N=1 then the following... (14 Replies)
Discussion started by: aemunathan
14 Replies

10. Solaris

Getting read only propmt when opening /etc/system file

root@atrcx146:/# vi /etc/system "/var/tmp/Exv9a4Rb" Read-only file system Please let me know the reason (1 Reply)
Discussion started by: Marty11
1 Replies
TRUNCATE(2)						      BSD System Calls Manual						       TRUNCATE(2)

NAME
truncate, ftruncate -- truncate a file to a specified length LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <unistd.h> int truncate(const char *path, off_t length); int ftruncate(int fd, off_t length); DESCRIPTION
truncate() causes the file named by path or referenced by fd to have a size of length bytes. If the file previously was larger than this size, the extra data is discarded. If it was previously shorter than length, its size is increased to the specified value and the extended area appears as if it were zero-filled. With ftruncate(), the file must be open for writing; for truncate(), the process must have write permissions for the file. RETURN VALUES
A value of 0 is returned if the call succeeds. If the call fails a -1 is returned, and the global variable errno specifies the error. ERRORS
Error return codes common to truncate() and ftruncate() are: [EISDIR] The named file is a directory. [EROFS] The named file resides on a read-only file system. [ETXTBSY] The file is a pure procedure (shared text) file that is being executed. [EIO] An I/O error occurred updating the inode. [ENOSPC] There was no space in the filesystem to complete the operation. Error codes specific to truncate() are: [ENOTDIR] A component of the path prefix is not a directory. [ENAMETOOLONG] A component of a pathname exceeded {NAME_MAX} characters, or an entire path name exceeded {PATH_MAX} characters. [ENOENT] The named file does not exist. [EACCES] Search permission is denied for a component of the path prefix, or the named file is not writable by the user. [ELOOP] Too many symbolic links were encountered in translating the pathname. [EFAULT] path points outside the process's allocated address space. Error codes specific to ftruncate() are: [EBADF] The fd is not a valid descriptor. [EINVAL] The fd references a socket, not a file, or the fd is not open for writing. SEE ALSO
open(2) STANDARDS
Use of truncate() to extend a file is an IEEE Std 1003.1-2004 (``POSIX.1'') extension, and is thus not portable. Files can be extended in a portable way seeking (using lseek(2)) to the required size and writing a single character with write(2). HISTORY
The truncate() and ftruncate() function calls appeared in 4.2BSD. BUGS
These calls should be generalized to allow ranges of bytes in a file to be discarded. BSD
March 16, 2008 BSD
All times are GMT -4. The time now is 06:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy