overwriting on a file


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers overwriting on a file
# 1  
Old 09-12-2012
overwriting on a file

I have loads of files in different directories which I do miner changed on them and till now I have to create totally new directories for them, specially as I deal with a whole directory at once SmilieSmilieSmilieSmilie
it creates such as mess really
e.g.
Code:
FILES="F1/*"
for X in $FILES
do
	name=$(basename $X) 
	sort  $X | uniq  | awk '{ $1=""}1' | awk '{ $NF=""}1'  > F2/${name} 
done

can it be written on the same files? on F1
can some one please let me know how I can write on output on the same exact file?

cheers

Last edited by A-V; 09-12-2012 at 07:26 PM..
# 2  
Old 09-13-2012
Any time you have a pipeline that reads from a file that is overwritten by a redirection at the end of the pipeline, you will lose data. The shell's will wipe out the current data in the file specified by the redirection before any command in the pipeline has a chance to read from it.

However, you could add:
Code:
        mv F2/$name /F1

after the pipeline starting with sort in your for loop as long as:
  1. There are no spaces, tabs, newlines, or other special characters in any of your filenames,
  2. there are no hard links to any of the file in directory F1, and
  3. you have sufficient space available on the file system.
If 1 or 3 above is not true, your script already has problems. If there are hard links to files in F1 that you need to preserve, your could replace the command added above with:
Code:
        cp F2/$name F1/$name && rm F2/$name

to preserve the links.

You could also simplify the and speed up your script if you replace:
Code:
sort  $X | uniq  | awk '{ $1=""}1' | awk '{ $NF=""}1'  > F2/${name}

in your loop with:
Code:
sort -u $X | awk '{ $NF="";$1=""}1' > F2/${name}

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-13-2012
very helpful... thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Logrotate with catalina.out "daily compressed file is overwriting the logs"

Hi Everyone, We are trying to do a logrotate for "catalina.out" daily, So have used the "copytruncate" option in the logrotate configuration but ended up seeing there was a 0kb file touched with "catalina.out" and the other file with the current date(compressed) file created and seems the log is... (2 Replies)
Discussion started by: thiyagoo
2 Replies

2. Shell Programming and Scripting

Avoid overwriting backup file when multiple entries need to replace in one file input from another

Hello, I have been working on script in which search and replace the multiple pattern. 1. update_params.sh read the multiple pattern from input file ParamMapping.txt(old_entry|New_entry) and passing this values one by one to change_text.sh 2. change_text.sh read... (0 Replies)
Discussion started by: ketanraut
0 Replies

3. Shell Programming and Scripting

sed is not overwriting a file

hi i have a file as in follwoing: cat apple.txt Apple is a fruit But this fruit is costly Now I used the sed command and i see output as in following # sed 's/fruit/healthy &/' fruit.txt Apple is a healthy fruit But this healthy fruit is costly # sed works fine here. But... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

File overwriting

Hi, An application is transferring a file to linux system with same file name. As file is transferring with same name always file will be overwritten. In this case we want to know what times file was overwirtten like below. Modify: 2014-05-12 00:52:01.000000000 Modify: 2014-05-12... (2 Replies)
Discussion started by: Satyak
2 Replies

5. Shell Programming and Scripting

Overwriting file with sftp

I have a script, which runs through cronjob every night 9 PM. It is supposed to do following tasks -- 1- Connect to ftp.testsite.com via user redtest 2- Fetch file red_bill.txt to my local server, where my script is residing. 3- Rename red_bill.txt to red_bill.V01.txt everyday on sftp server.... (4 Replies)
Discussion started by: solaris_1977
4 Replies

6. Linux

Is it possible to revert a file after overwriting it ?

Long story short, there was some sort of corruption with my ide and the script I was working on has been over written with nothing (the file is blank now). The IDE doesn't store a back up from what I know (I'm using notepadd++ in wine lol I know I know I'm addictted to the nppftp sidebar and geany... (1 Reply)
Discussion started by: noPermissions
1 Replies

7. Programming

Copying and overwriting a file using file descriptor

Hi , i have two basic requirement on linux platform . I am using C language to do this . 1) copying one file to another (assuming i know their file descriptors) 2) Overwriting a file using it file descriptor . Please guide. regards Aki (2 Replies)
Discussion started by: meet123321
2 Replies

8. Shell Programming and Scripting

Overwriting File

How to overwrite every time a particular portion of a file? ---------- Post updated at 02:16 PM ---------- Previous update was at 02:07 PM ---------- I have a file whose contents are something like: Output of "apachectl fullstatus" command: ---------------------------------------------... (3 Replies)
Discussion started by: proactiveaditya
3 Replies

9. Shell Programming and Scripting

successive file renaming & overwriting

Hi all, In our webserver customer wants to see the latest 10 files arrived. So the names are hardcoded in webpage like : filename_01.txt filename_02.txt .... .... filename_10.txt where the filename_01.txt is the latest one (typically the output of ls -1t) in the /../webpage directory.... (2 Replies)
Discussion started by: sabyasm
2 Replies

10. UNIX for Dummies Questions & Answers

Does "gzip" have a no prompt option on it for overwriting if file exists?

So I dont enounter things like: gzip: /sometimename.gz already exists; do you wish to overwrite (y or n)? Want to add it into a script and if there is a file aready there to just overwrite it, otherwise the script will hang unless there is manual intervention. (1 Reply)
Discussion started by: LordJezo
1 Replies
Login or Register to Ask a Question