Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 01-05-2010
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
# 2  
Old 01-05-2010
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)
# 3  
Old 01-05-2010
Or if your sed version supports the -i (inplace) option (check your manpage):
Code:
sed -i 's/hello/bye/g' test.dat

# 4  
Old 01-05-2010
brilliant, thanks
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search and replace in file sunnu2u86 Shell Programming and Scripting 12 10-05-2016 06:11 PM
Nested search in a file and replace the inner search tolearn Shell Programming and Scripting 2 03-03-2014 12:02 AM
awk search/replace specific field, using variables for regexp & subsitution then overwrite file cyphex Shell Programming and Scripting 3 09-23-2012 05:46 AM
search and replace in csv file dondilip Shell Programming and Scripting 30 02-28-2012 06:34 AM
Help on search and replace in a file firestar Shell Programming and Scripting 1 07-19-2011 03:21 PM
Find ,replace and overwrite from master and sub dir pasupuleti81 Shell Programming and Scripting 3 11-16-2010 01:35 AM
Search and replace in xml file using awk.. islam2666 Shell Programming and Scripting 4 02-28-2010 09:08 PM
search and replace in a file ammu UNIX for Advanced & Expert Users 3 06-17-2009 05:20 AM
Search and replace in file.. Amits Shell Programming and Scripting 2 08-22-2006 07:11 PM
Search and replace in file malaymaru UNIX for Dummies Questions & Answers 3 05-03-2005 06:57 AM


All times are GMT -4. The time now is 06:57 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password