editing file in place


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting editing file in place
# 1  
Old 09-08-2008
editing file in place

Is in Unix a tool that could do this:

paste -d":" 'file1' 'file2' | special_save "file1" # so that file1 would be treated in a way that would prevent problems with race condition

I know it normally doesn't work but is there any intelligent way how to avoid using temporary file? I know methods like heredoc, redirections,... but still it's not too comfortable..

Thank you!

Last edited by MartyIX; 09-08-2008 at 09:47 AM..
# 2  
Old 09-08-2008
Never heard of such a tool, but what's wrong with:

Code:
paste -d":" file1 file2 > temp.file && mv temp.file file1

Regards
# 3  
Old 09-08-2008
You can hide the temp file handling behind some sort of wrapper.

Code:
inplace () {
  local tmp dest
  tmp=`mktemp -t inplace.XXXXXX`
  file=$1
  shift
  "$@" >"$tmp"
  mv "$tmp" "$file"
}

inplace file1 paste -d ":" file1 file2

One clever workaround I stumbled on in these forums a few weeks ago used backticks to get the data out of the destination file before overwriting it.

Code:
echo "`paste -d ":" file1 file2`" >file1

# 4  
Old 09-08-2008
Franklin52: well, I use this solution often but it seems to me a little weird to create a temporary file and rename it right away. I would expect this line:

Code:
paste -d":" 'file1' 'file2' | special_save "file1"

just more natural..

And if I'm not wrong I need one more disk access with renaming than I would need with the command above.
# 5  
Old 09-08-2008
2era:
> One clever workaround I stumbled on in these forums a few weeks ago used backticks to get the data out of the destination file before overwriting it.

Thank you! I like the solution :-) but it would not work properly with escape sequences, would it? Chars like "\n" are not treated as escape sequence unless switch -e is enabled - and this switch is not in single unix specification.

I think that:

Code:
variable="`paste -d ":" file1 file2`"
printf "%s" $variable >file1

should work always. Am I right?

One more thing racks my brains: How long text may I set to a variable?
# 6  
Old 09-08-2008
You are right that echo is not portably safe, but then printf might not be available on legacy platforms either. If it's available, it's a good solution.

I have a var="`dd if=/dev/zero`" running in another window. So far, it's looking like it's only constrained by available memory. Actually I think I'll kill it now ... 11GB so far, with no particular indication that we would hit the limit any time soon. But this is probably highly platform-dependent.
# 7  
Old 09-08-2008
oh, printf is part of GNU project, I didn't know that.

I was affraid of much lower limits than 11GB ... it's ok then :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a file in all directories and place the file in that directory

Hi All, Daily I am getting the updated file. I have to search for this file in all directories and sub directories. If the file existed in a particular directory then move this updated file to that particular directory. If the file is not existed in any of the directories then place this... (4 Replies)
Discussion started by: ROCK_PLSQL
4 Replies

2. Shell Programming and Scripting

Check file availability and place flag file

I have to check a directory on Linux (via shell Script which I am trying to build) for about 20 different source files with file patterns and if the files are made available in the directory, I should place flag files for which my other ETL jobs are waiting on to kick off. If the source files are... (6 Replies)
Discussion started by: dhruuv369
6 Replies

3. Shell Programming and Scripting

Cut text file in place

I have a file that i want to take only the first part of it and discard the rest, to be accurate,I need the first 137097 lines but I cant use split because I dont have enough space on my disck. I need sth to cut the file in its place (3 Replies)
Discussion started by: Heidi Heweidy
3 Replies

4. UNIX for Dummies Questions & Answers

how to place input from user into a file

as the title, how can i allow a user to key in one's name, birthday and email address and save it into a specific file i have created earlier? thank you so much for any reply. (3 Replies)
Discussion started by: branred
3 Replies

5. Shell Programming and Scripting

Read from file specific place in file using inode

Hello, I am using tcsh on AIX. I would like to write a script that does the following: 1. given an inode, how do I find exactly the name of the file? I know I could do this using ls -i | grep <inode> but it returns: <inode> <filename>. I need some string manipulation or something to... (1 Reply)
Discussion started by: lastZenMaster
1 Replies

6. UNIX for Dummies Questions & Answers

Copy dir/file from one place to another.

Hello all. I'm not getting the hang of Paths. I have a dir w/files that I want to copy to another dir. Right now I am in the "source" directory. I want to copy it to Ch7. "cp -r source Ch7". Ch7 was already created. 1st msg.: cannot stat `source`: No such file or dir. I typed pwd & got... (3 Replies)
Discussion started by: Ccccc
3 Replies

7. Solaris

What is the best way to copy data from place to another place?

Dear Gurus, I need you to advice or suggestion about the best solution to copy data around 200-300G from serverA(location A) to serverB(location B). Normally, I will share folder and then copy but it takes too long time(about 2 days). Do you have any suggestion or which way should be... (9 Replies)
Discussion started by: unitipon
9 Replies

8. Shell Programming and Scripting

How to insert a string in a file at specified place?

Hi all, I want to insert a string in a specified place of a very large file. I am giving an example of the task: I love football. Above is a sentence in a file and I want to insert a string "the" between love and football. It is not sure that where this particular line exists. It has to... (4 Replies)
Discussion started by: naw_deepak
4 Replies

9. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 Replies

10. Shell Programming and Scripting

Edit a large file in place

:confused:Folks, I have a file with 50 million records having 2 columns. I have to do the below: 1. Generate some random numbers of a fixed length. 2. Replace the second column of randomly chosen rows with the random numbers. I tried using a little bit of perl to generate random numbers... (6 Replies)
Discussion started by: mvijayv
6 Replies
Login or Register to Ask a Question