File editing with out creating a new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File editing with out creating a new file
# 1  
Old 11-03-2012
IBM File editing with out creating a new file

I need edit some characters in a file, but without creating intermediatory file and also one liner.

I tried:

Code:
cat foo.txt | sed '/s/abc//g' > foo.txt
cat foo.txt | sed '/s/abc//g' >> foo.txt

First one is making foo.txt to zero byte, while second one is appending my desired output to existing content.

Actually I done have -i option in sed.

Any otherway to implement this ?

Thanks!
# 2  
Old 11-03-2012
Code:
perl -i -pe 's/abc//g' foo.txt

# 3  
Old 11-03-2012
karumudi7, that is useless use of cat. sed is capable of reading the file by itself.

Another way of doing it (without -i option):-
Code:
sed -e 's/abc//g' foo.txt > foo.tmp && mv foo.tmp foo.txt

Using ed:-
Code:
printf "%s\n" '1,$s/abc//g' w q | ed foo.txt


Last edited by Yoda; 11-03-2012 at 01:01 PM..
# 4  
Old 11-03-2012
File editing with out creating a new file

Code:
sed -i 's/this/that/g' foo

will do it

Last edited by Franklin52; 11-05-2012 at 03:13 AM.. Reason: Please use code tags for data and code samples
# 5  
Old 11-03-2012
@grizzledtop: he already said in his first post that his sed doesn't have the "-i" flag. Only GNUs "sed" has this so its usage is strongly discouraged if you want to write portable scripts.

You might want to read this to understand why it is impossible.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in creating a file in required format form another existing file

I have a text file with contents like this: a,b,c, d~e,f,g,h~i,j ,k,l,m~n,o,p,q~ I need to convert this file into this format unix shell script commands: a,b,c,d~ e,f,g,h~ i,j,k,l,m~ n,o,p,q~ as you may have noticed, I need to retain the ~ signs at the end. Any help is greatly... (3 Replies)
Discussion started by: harsha1238
3 Replies

2. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

3. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

4. Shell Programming and Scripting

Help with creating a text file in perl with file creation date.

Hi, I am quite new to Perl scripting and i need to create a .TXT file using perl, with fields (A,B,C,D,E), and this text file should be named with current file creation date "XYZ_CCYYMMDD.TXT" (i.e.XYZ_2011042514:33 PM). Can anyone who has done this, please share their expertise on this... (5 Replies)
Discussion started by: msrahman
5 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

7. UNIX for Advanced & Expert Users

creating a file by editing another one?

BASH shell: I'm trying to create a copy of the /etc/passwd file named "./copy-passwd" that contains only the entries for userids that have a home directory in /home/inst, and then change each entry in the copy so that the default login shell is /bin/sh. Would "sed" be the best choice of file... (1 Reply)
Discussion started by: raidkridley
1 Replies

8. Shell Programming and Scripting

ls > file - Creating file containing the list of all files present in a directory

Hi All, I need to create a file which contains the list of all the files present in that directory. e.g., ls /export/home/user/*.dat > list_file.dat but what i am getting is: $ ls /export/home/user/*.dat > list_file.dat /export/home/user/*.dat: No such file or directory But I have... (1 Reply)
Discussion started by: pranavagarwal
1 Replies

9. Ubuntu

Avoid creating temporary files on editing a file in Ubuntu

Hi, My ubuntu flavor always create temporary files having filename followed by ~ on editing. For eg: if I am editing a file called "sip.c", automatically a temporary (bkup) file is getting created with the name "sip.c~". How to avoid this file creation? (7 Replies)
Discussion started by: royalibrahim
7 Replies

10. UNIX for Advanced & Expert Users

Editing the end of the file without loading the entire file

hi! I am a newbee. I would really appreciate if you can answer the following question: I have a huge data file, 214MB with several coloumns. I need to delete the very last line of the file. Everything I know takes a lot of time to do it ( because I have to open the file in an editor or run a... (3 Replies)
Discussion started by: Garuda
3 Replies
Login or Register to Ask a Question