Using cut without having to rename file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using cut without having to rename file
# 1  
Old 07-14-2011
Using cut without having to rename file

I am using the cut command as follows:
Code:
cut -d " " -f 3 file.dat > file_.dat

Is there a way to not have to rename the file, so that the result is actually written into file.dat directly within the cut-command? I could do a mv file_.dat file.dat, but that would be an additional step.
# 2  
Old 07-14-2011
Something like this should do (didn't test it):
Code:
perl -i -lane 'print $F[2]' file.dat

# 3  
Old 07-14-2011
Quote:
Originally Posted by figaro
I could do a mv file_.dat file.dat, but that would be an additional step.
There's not a 1-step way to do that unless the application's actually designed to do that, and most aren't. cut isn't. Those that are frequently side-effects.

sed -i, for instance, can change the ownership and permissions of the files it operates on because it's two steps -- create new file, replace the old. Run sed -i on a user as root, it'll replace the user-owned file with one owned by root!
# 4  
Old 07-14-2011
Quote:
Originally Posted by figaro
Is there a way to not have to rename the file, so that the result is actually written into file.dat directly within the cut-command? I could do a mv file_.dat file.dat, but that would be an additional step.
I'm curious. Why do you even care about a subsequent mv step? Is there some constraint in play that you neglected to mention?

In case you've suffered mv-trauma in the past and it's in your best mental-health interests to avoid it, I suppose you could 'cat file_.dat > file.dat; rm file_.dat`. Technically, that does not involve any renaming.

There's also the unlink trick, which leverages the kernel's unwillingness to deallocate a file so long as a process maintains an open descriptor on it, even when that file no longer exists as an entry in the filesystem:
Code:
{ rm file.dat; cut -d " " -f 3 > file.dat; } < file.dat

Although that doesn't really save you any work. It still creates a new file and a new entry must be linked into the directory (the inode number for file.dat changes) and it still needs to fork/exec another utility to get the job done (rm). Worse, it creates a small window during which a system failure will leave you without any version of the data, and a larger window during which you'll have only a partially complete version of the data.

Are you feeling lucky? Smilie

Regards,
Alister
# 5  
Old 07-15-2011
HI.

The thread at https://www.unix.com/shell-programmin...rd-output.html demonstrates a Linux utility, sponge, and a perl work-alike that writes on the input file safely if desired -- it collects the output until EOF, then re-writes the file as specified. The cost is the time and space needed to collect the data on a temporary file and copy that file.

That is a solution that can be applied to this general class of problems.

Best wishes ... cheers, drl
# 6  
Old 07-18-2011
Thanks all for your answers. I will settle for an additional mv command, since it is the easiest to understand for a novice programmer.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

4. UNIX for Dummies Questions & Answers

look for specific values in a file and rename file with value found

Hi, i have a file with some data ..look for some specific value in the file and if found that value rename the file with the value found in the file.. ex.. File.txt 1236 43715825601ANDERSSON, 1236 437158256031963040120060901200609010000000 1236 43715825604123 MCCL AVE UPPER 1236 ... (11 Replies)
Discussion started by: dssyadav
11 Replies

5. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

6. UNIX for Dummies Questions & Answers

Rename file based on first 3 characters of data in file

I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. An example is: Read FileA If the first 3 positions of the data in the file are "ITP", then rename the file as FileA_ITP, else if the first 3... (3 Replies)
Discussion started by: jchappel
3 Replies

7. Shell Programming and Scripting

Rename a file name

Hi, I am getting a file name from a remote server thru ftp and storing this filename in a variable. I am doing this in ftp mode. for ex: the filename is like emp_010208. This file name has to be changed to emp. Can you please let me know how we can do this while we are in ftp mode. thanks,... (0 Replies)
Discussion started by: Aswarth
0 Replies

8. UNIX for Dummies Questions & Answers

File rename

Hi, I need to rename a file like this.. Original File : $orderFile =SYLVATICAINC_12076767647_D000005267_1_116695689.ps New File Name SYLVATICAINC_1333333333_D000005267_1_116695689.ps My ip put is 33333333 and 5267 cp $orderFile $orderFilepath/1+$newFax+_D00000+$orderno+_1_*.ps;... (1 Reply)
Discussion started by: redlotus72
1 Replies

9. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question