Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-11-2012
Registered User
 
Join Date: Nov 2009
Posts: 25
Thanks: 14
Thanked 0 Times in 0 Posts
String replace

I want to replace the last date pattern with the current day date

my file looks like

Code:
>cat sample.dat
1,parameter 2012-06-10
2,parameter 2012-06-10

I want in the below format

Code:
1,parameter 2012-06-11
2,parameter 2012-06-11

I tried

Code:
sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$/`date +"%Y-%m-%d"/g`' sample.dat

this is not working ...can any body help me

Moderator's Comments:
Please use code tags, thanks!

Last edited by zaxxon; 06-11-2012 at 04:13 AM.. Reason: code tags
Sponsored Links
    #2  
Old 06-11-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,561
Thanks: 14
Thanked 438 Times in 423 Posts

Code:
sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$/'`date +"%Y-%m-%d"`'/g' sample.txt

The Following User Says Thank You to balajesuri For This Useful Post:
midhun19 (06-13-2012)
Sponsored Links
    #3  
Old 06-13-2012
Registered User
 
Join Date: Nov 2009
Posts: 25
Thanks: 14
Thanked 0 Times in 0 Posts
i have tried


Code:
sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$/'`date +"%Y-%m-%d"`'/g' sample.txt

also i tried
Code:
sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$//g' sample.txt

but both showing me the same results ...


Code:
1,parameter 2012-06-10
2,parameter 2012-06-10

i think the regular expression is not correct.. please help

Last edited by Scrutinizer; 06-13-2012 at 04:01 AM.. Reason: code tags
    #4  
Old 06-13-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,341
Thanks: 144
Thanked 1,754 Times in 1,591 Posts
Check if your input file is in DOS format, it should be in Unix format.
The Following User Says Thank You to Scrutinizer For This Useful Post:
midhun19 (06-13-2012)
Sponsored Links
    #5  
Old 06-13-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 476 Times in 456 Posts
The shell will not look at your date command which you've protected using single-quotes...

Try this


Code:
sed "s/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$/`date +"%Y-%m-%d"`/g" e123


Last edited by elixir_sinari; 06-13-2012 at 04:31 AM..
The Following User Says Thank You to elixir_sinari For This Useful Post:
midhun19 (06-13-2012)
Sponsored Links
    #6  
Old 06-13-2012
Registered User
 
Join Date: Nov 2009
Posts: 25
Thanks: 14
Thanked 0 Times in 0 Posts
when i echo a line and pipe it to sed...its working

> echo "1,parameter 2012-06-10" | sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$/'`date +"%Y-%m-%d"`'/g'
1,parameter 2012-06-13

but when i use the file like

cat sample.txt | sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$/'`date +"%Y-%m-%d"`'/g'
or
sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$/'`date +"%Y-%m-%d"`'/g' sample.txt

its not working

---------- Post updated at 05:27 AM ---------- Previous update was at 05:24 AM ----------

can you please tell me, how to distinguish a dos format file and a unix format...
the file which im using is created by vi editor in unix
Sponsored Links
    #7  
Old 06-13-2012
Registered User
 
Join Date: May 2012
Posts: 58
Thanks: 5
Thanked 9 Times in 9 Posts
Execute the following command:


Code:
dos2unix sample.txt

If the file is in dos format it will be converted to unix format
Sponsored Links
Closed 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
replace (sed?) a string in file with multiple lines (string) from variable jforce Shell Programming and Scripting 2 09-15-2011 10:10 AM
awk - replace number of string length from search and replace for a serialized array otrotipo Shell Programming and Scripting 1 07-10-2009 12:04 PM
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 06:11 AM
Search for a string and replace the searched string in the same position in samefile ganesh_248 UNIX for Dummies Questions & Answers 27 03-23-2009 12:35 AM
Search for a string and replace the searched string in the same position ganesh_248 Shell Programming and Scripting 15 02-09-2009 08:35 PM



All times are GMT -4. The time now is 04:50 AM.