sed, date and /&


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed, date and /&
# 1  
Old 01-01-2008
sed, date and /&

Hi,

Code:
$ echo 1 titi | sed -e "s/1/$(echo \&)/"
1 titi

but

Code:
$ echo 1 titi | sed -e "s/1/$(date \&)/"
date: invalid date `&'
 titi

how can i do for handle '\&' with date ?

Thx
# 2  
Old 01-01-2008
What exactly are you trying to achive with this code?
# 3  
Old 01-19-2008
Use case is not important, this is just one example.

I just wanna use the seed replacement special character & with the date command, like with echo.
# 4  
Old 01-19-2008
You are passing an invalid argument to the date utility as in "date 1", hence the error.

Code:
$ echo 1 titi | sed -e "s/1/$(date \&)/"
date: illegal time format
usage: date [-ajnu] [-d dst] [-r seconds] [+format]
       date -b [-ajnu] [-d dst] [-t west] [+format] [[[[[[cc]yy]mm]dd]HH]MM[.SS]
       date [-ajnu] [-d dst] [-t west] [+format] [mmddHHMM[[cc]yy]]
 titi

# 5  
Old 01-20-2008
fpmurphy, thx for the reply.

With a good argument, that still doesn't work for me.

Code:
echo 01201300 titi | sed -e "s/01201300/$(date \&)/"

date: invalid date `&'
 titi

GNU bash, version 3.2.25(1)-release (x86_64-pc-linux-gnu)
# 6  
Old 01-20-2008
Do you want an output line this?

Code:
$ echo 08150000 titi | sed "s/08150000/$(date &)/"
Fri Aug 15 00:00:08 WEDT 2008 titi

This will change the machine's date Smilie

Last edited by radoulov; 01-20-2008 at 10:07 AM..
# 7  
Old 01-20-2008
sncr24, your question does not make sense. That's why we cannot answer it.
Code:
$ echo 1 titi
1 titi
$ echo 1 titi | sed "s/1/&/"
1 titi

Those 2 command lines have the same output because the sed command makes no change to the text. The ampersand represents the matched field. Normally, you might do something like this...
Code:
$ echo 1 titi | sed -e "s/1/=&=&=/"
=1=1= titi
$

Getting back the the original sed command, which already does nothing, you can replace one of the characters in the sed command with an echo statement and that is what you did with
Code:
$ echo 1 titi | sed -e "s/1/$(echo \&)/"
1 titi

This continues to give the same output as a simple "echo 1 titi" but it watses even more time. Replacing the echo statement with an invocation of date is even more strange, but it can be done because "date" can be forced to behave a little bit like "echo".
echo \&
date +\&
both output a single ampersand. So both
Code:
echo 1 titi | sed -e "s/1/$(echo \&)/"
echo 1 titi | sed -e "s/1/$(date  +\&)/"

are ways to construct a sed command that will not do anything. But even though we got date to behave a liitle bit like echo, this won't be possible with all of the other unix commands.

I do not know what you are try to do, but I hope this helps move you toward fufilling your requirements. (Whatever they may be...) Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Echo "abc" | sed - r 's/a/&_&/

I want to know the working of & here step by step using sed command. (1 Reply)
Discussion started by: Preeti07
1 Replies

2. UNIX for Dummies Questions & Answers

Print start date to end date, given $1 & $2 in ksh

Dear all, I have an user passing 2 parameter 31/03/2015 and 02/04/2015 to a ksh script. How to print the start date to end date. Expected output is : 31/03/2015 01/04/2015 02/04/2015 Note : 1. Im using aix and ksh 2. I have tried to convert the given input into a date, didnt... (0 Replies)
Discussion started by: mr.rajaravi
0 Replies

3. Shell Programming and Scripting

How to list files that are not first two files date & last file date for every month-year?

Hi All, I need to find all files other than first two files dates & last file date for month and month/year wise list. lets say there are following files in directory Mar 19 2012 c.txt Mar 19 2012 cc.txt Mar 21 2012 d.txt Mar 22 2012 f.txt Mar 24 2012 h.txt Mar 25 2012 w.txt Feb 12... (2 Replies)
Discussion started by: Makarand Dodmis
2 Replies

4. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

5. Shell Programming and Scripting

Calculating expiry date using date,sed,grep

Hi, I would greatly appreciate it if someone can help me with my problem. I have a crawler which collects spam URLs everyday & this data needs to be published in a blacklist. Here's the catch: The "Time To Live" (TTL) for each URL is 3 months (or whatever for that matter). If i see the... (5 Replies)
Discussion started by: r4v3n
5 Replies

6. Programming

DATE & TIme

Can we select the datetime from oracle database in “hhmmssnnnccyymmdd” format ? please help to solve this..... (2 Replies)
Discussion started by: Sanal
2 Replies

7. Shell Programming and Scripting

sed & areas respectively sed & pyramiding

Hello everyone, i wonder if someone could give me an advice regarding the following problem using sed. Given ist a structure as shown below: <aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa> Now I want to change the outer tag from "aaa" to "new" and replace all tags inside the outer tags... (4 Replies)
Discussion started by: Donaldinho
4 Replies

8. Shell Programming and Scripting

Timestamp & date

Hi, I have list of files as below, with prefix named as date & time. Anyone how to transform each file as below to yyyy-mm-dd hh:mm:ss Regards, (8 Replies)
Discussion started by: rauphelhunter
8 Replies

9. What is on Your Mind?

[[ $(date +%Y) == 2007 ]] && echo "Happy New Year"

Same as the Title! :) (2 Replies)
Discussion started by: ripat
2 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question