Replace String without using sed

Tags
replace, replace string, sed, shell scripts, string

 
Thread Tools Search this Thread
# 1  
Old 10-08-2018
Replace String without using sed

Hi Team,

I have a file and need to replace string. Out of 20 rows, there is one row given below

Code:
$Paramsoqlfilter=Systemmodstamp > 1900-01-01T00:00:00.000Z

in a Shell Script, I have a variable
Code:
HIST_DATE="1900-01-01T00:00:00.000Z"
INC_DATE="2018-10-04T09:18:43.000Z"

Now I need to replace HIST_DATE with INC_DATE.

That mean, the string in a file should get INC_DATE like below

Code:
$Paramsoqlfilter=Systemmodstamp > 2018-10-04T09:18:43.000Z

Issue: sed -i not available. I'm suing korn shell

Can someone please help me?
# 2  
Old 10-08-2018
you don't need to have '-i' enabled sed to edit the file.
what's your file look like?
what do you have so far to edit the file so far (with or without sed)?
# 3  
Old 10-09-2018
Code:
HIST_DATE="1900-01-01T00:00:00.000Z"
INC_DATE="2018-10-04T09:18:43.000Z"

ex input_file << EDIT
1,$ s/$HIST_DATE/$INC_DATE/g
w!
EDIT

This User Gave Thanks to rdrtx1 For This Post:
jim mcnamara (10-11-2018)
# 4  
Old 10-09-2018
You can also use perl, which should be pre-installed on any *nix disto:

Code:
perl -p -i -e 's/$HIST_DATE/$INC_DATE/g' /path/to/file


That's may be easier to commit to memory if you're used to sed -i.
# 5  
Old 10-09-2018
Shell only:
Code:
while read -r line
do
  case $line in 
    (*"${HIST_DATE}") 
      line=${line#"${HIST_DATE}"}${INC_DATE}
    esac
  printf "%s\n" "$line"
done < file > file.new


--
bash / ksh93 / zsh:
Code:
while read -r line; do
  printf "%s\n" "${line/"${HIST_DATE}"/"${INC_DATE}"}"
done < file > file.new



----
Quote:
Originally Posted by Azrael
You can also use perl, which should be pre-installed on any *nix disto:

Code:
perl -p -i -e 's/$HIST_DATE/$INC_DATE/g' /path/to/file


That's may be easier to commit to memory if you're used to sed -i.
You would need to use double quotes for this to work..

Last edited by Scrutinizer; 10-09-2018 at 11:13 PM.. Reason: Added -r to read commands
# 6  
Old 10-11-2018
Quote:
You would need to use double quotes for this to work..
Maybe for the variable, but for straight substitution single quotes work just fine:

Code:
$ cat test
OK
$ perl -p -i -e 's/OK/NOTOK/' test
$ cat test
NOTOK

# 7  
Old 10-11-2018
Hi, with sed without -i :
file to modify:
Code:
$ cat /tmp/file.tst 
$Paramsoqlfilter=Systemmodstamp > 1900-01-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-01-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-01-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-01-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z

Code:
$ HIST_DATE="1900-01-01T00:00:00.000Z"
$ INC_DATE="2018-10-04T09:18:43.000Z"
$ exec 3</tmp/file.tst 
$ rm /tmp/file.tst 
$ sed -e "s/${HIST_DATE}/$INC_DATE/" <&3 >/tmp/file.tst
$ exec 3<&-

Code:
$ cat /tmp/file.tst 
$Paramsoqlfilter=Systemmodstamp > 2018-10-04T09:18:43.000Z
$Paramsoqlfilter=Systemmodstamp > 2018-10-04T09:18:43.000Z
$Paramsoqlfilter=Systemmodstamp > 2018-10-04T09:18:43.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 2018-10-04T09:18:43.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z
$Paramsoqlfilter=Systemmodstamp > 1900-02-01T00:00:00.000Z

Regards.
This User Gave Thanks to disedorgue For This Post:
MadeInGermany (10-12-2018)

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Using sed to replace string oferg Shell Programming and Scripting 1 12-05-2016 07:53 AM
Replace string in XML file with awk/sed with string from another cozzin Shell Programming and Scripting 9 02-15-2016 03:38 PM
sed String Replace thursday_g0ne Shell Programming and Scripting 4 06-12-2015 02:05 AM
sed or awk command to replace a string pattern with another string based on position of this string vivek d r Shell Programming and Scripting 10 06-19-2012 10:35 AM
String replace With Sed Anu_1 Shell Programming and Scripting 11 02-29-2012 05:29 AM
replace (sed?) a string in file with multiple lines (string) from variable jforce Shell Programming and Scripting 2 09-15-2011 11:10 AM
Replace exactly a string for another using sed jgrosecl UNIX for Dummies Questions & Answers 4 07-05-2011 05:02 PM
Sed - replace in the string tomix Shell Programming and Scripting 8 03-23-2011 08:34 PM
Using sed to replace one string with other sbhuvana20 UNIX for Dummies Questions & Answers 6 02-16-2011 05:23 AM
sed replace string vino_hymi UNIX for Dummies Questions & Answers 2 04-08-2010 05:49 AM
How to use sed to replace the a string in the same file using sed? gomes1333 Shell Programming and Scripting 7 03-27-2010 02:59 PM
Using sed to replace a string in file with a string in a variable that contains spaces vivelafete Shell Programming and Scripting 2 11-24-2009 09:43 AM
sed: replace string with another string (with spaces) zmfcat1 Shell Programming and Scripting 4 07-16-2009 06:37 AM
Is it possible to replace more the 1 string with one SED? tuathan UNIX for Dummies Questions & Answers 1 11-20-2008 10:12 PM
SED Replace String Help prash184u Shell Programming and Scripting 2 01-23-2008 01:57 AM