Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk search and replace and overwrite file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-05-2010   -   Original Discussion by JamesByars
JamesByars's Unix or Linux Image
JamesByars JamesByars is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 22 November 2010, 7:05 AM EST
Posts: 153
Thanks: 9
Thanked 0 Times in 0 Posts
awk search and replace and overwrite file

hi,

i am doing


Code:
awk '{gsub("hello", "bye", $0); print}' test.dat

but I want to actually store the results of this global change into the file test.dat itself, i.e. I don't want the results to come to stdout, I want the results to overwrite the initial file

how can I do this?

thanks
Sponsored Links
    #2  
Old Unix and Linux 01-05-2010   -   Original Discussion by JamesByars
radoulov's Unix or Linux Image
radoulov radoulov is offline
Registered User
 
Join Date: Jan 2007
Last Activity: 9 January 2017, 4:40 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 630 Times in 587 Posts
With AWK you'll need something like this:


Code:
awk '{gsub("hello", "bye")}1' test.dat > test.dat_tmp && 
  mv test.dat_tmp test.dat

or this (not recommended):


Code:
{ rm test.dat && 
    awk '{gsub("hello", "bye")}1' > test.dat
  } < test.dat

I would use Perl:


Code:
perl -i -pe's/hello/bye/g' test.dat

The Following User Says Thank You to radoulov For This Useful Post:
firefox2k2 (07-21-2015)
Sponsored Links
    #3  
Old Unix and Linux 01-05-2010   -   Original Discussion by JamesByars
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 5 December 2017, 5:22 PM EST
Location: The Netherlands
Posts: 7,746
Thanks: 139
Thanked 558 Times in 520 Posts
Or if your sed version supports the -i (inplace) option (check your manpage):

Code:
sed -i 's/hello/bye/g' test.dat

    #4  
Old Unix and Linux 01-05-2010   -   Original Discussion by JamesByars
JamesByars's Unix or Linux Image
JamesByars JamesByars is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 22 November 2010, 7:05 AM EST
Posts: 153
Thanks: 9
Thanked 0 Times in 0 Posts
brilliant, thanks
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search and replace in a file ammu UNIX for Advanced & Expert Users 3 06-17-2009 06:20 AM
how can search a String in one text file and replace the whole line in another file kkraja UNIX for Dummies Questions & Answers 6 08-06-2008 08:23 AM
search and replace in a file satyam_sat Shell Programming and Scripting 2 04-11-2008 02:28 AM
Search and replace in file.. Amits Shell Programming and Scripting 2 08-22-2006 08:11 PM
Search and replace in file malaymaru UNIX for Dummies Questions & Answers 3 05-03-2005 07:57 AM



All times are GMT -4. The time now is 09:20 AM.