Using commands inside sed substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using commands inside sed substitution
# 1  
Old 09-20-2011
Using commands inside sed substitution

Dear Friends,

Please let me know how to use the date command inside the substitution flag replacement string.

echo "01 Jan 2003:11:00:06 +0100" | sed 's/\(.*\)/`date -d \1 "+%Y%m%d%H%M%S"`/'

I want to supply \1 string to Here mention below as part of replacement string,

date -d <Here> "+%Y%m%d%H%M%S"

However my sed version is not working. please let me know the right way to do this.

Thanks in advance,
# 2  
Old 09-20-2011
I understand you are trying to replace the entire statement with the date command. In that case,

Code:
$ DATE=`date -d \1 "+%Y%m%d%H%M%S"`

$ echo "01 Jan 2003:11:00:06 +0100" | sed "s/.*/$DATE/"
20110921010000

Guru.
# 3  
Old 09-20-2011
Guruprasad,

Thanks for your response. However I am not looking this one. I exactly want to know how to use date command in substitution replace string. a log file will be read from the pipeline where each line will have a date string which has to be replaced with the date in different format(Numerical).
# 4  
Old 09-20-2011
I don't think sed will work in this situation. Something like this is more along the lines of what you'll need. It assumes the date you want to transform is the first 5 tokens on each record. It can easily be adapted to convert the date from anyplace in the string, but as most log files contain the date up front, I assumed it was first.

Code:
 awk '
    function execute( cmd,  r )   # execute command, return last line of output from command. 
    {
        while( (cmd | getline) )
            r = $0;

        close( cmd );    # very important to close the pipe
        return r;
    }

    {
        $5 = $5 "~";    # assume fields 1-5 are the date
        split( $0, a, "~" );
        new_date = execute( sprintf( "date -d \"%s\" \"+%%Y%%m%%d%%H%%M%%S\"", a[1] ) );
        print new_date  a[2];
    }
' log-file-name

# 5  
Old 09-20-2011
Code:
$ echo "NNabcNN"  | sed -e 's/abc/'"`date`"'/g'
NNTue Sep 20 22:22:53 EDT 2011NN

# 6  
Old 09-21-2011
fpmurphy,

Thanks for your response. Your suggestion partially satisfies my requirement. However Here abc will be replaced by the current date always. I want to pass "abc" as the input to the data command in the replacement string so that the date will be displayed with respect to "abc" (abc has to be replace by the log date)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vi quick substitution commands

I have a script that has quite a lot of these types of entries: hello=$(who am i) good=$(blahblah) what i want to do is replace the $( and the ) with `` so that they look like this: hello=`who am i` good=`blahblah` I tried this: :%s~=$(&&)$~=`&&`$~g (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

RMAN commands inside crontab shell script

Hello I'm trying to write simple script to delete archive logs for RMAN, unfortunately it's not working, I tried two way to do that: #!/bin/ksh echo "Start ....." rman target=/ << EOF RUN { delete force noprompt archivelog until time 'sysdate-10'; } EXIT; EOF echo "END ..." echo... (6 Replies)
Discussion started by: samer.odeh
6 Replies

3. Shell Programming and Scripting

Command substitution inside of a variable expression (AIX, KORN)

Hello all. This is my first post/question on this site. I’m a new Systems Analyst with previous experience with BASH. Although now I'm using AIX, and I’m trying to get a feel for the Korn shell (for those of you that don’t know AIX only uses the KORN shell). I hope I put this into the correct... (10 Replies)
Discussion started by: sydox
10 Replies

4. Shell Programming and Scripting

Automating execution of commands inside a program

I have a program dnapars I execute the program from command line as following: ./dnapars The program then prompts me some message as a user menu from where I have to select a series of options in the order R U Y R. And then I copy the output file (outfile) in another result file. I wrote the... (3 Replies)
Discussion started by: deeptisjains
3 Replies

5. Shell Programming and Scripting

Using Sed to do a substitution inside ksh

I'm trying to do an ls from inside of a ksh script. I loop through the results one line at a time and attempt to do a substitution using sed to convert YYYYMMDD from the older files into the newer files. Basically sometimes the ETL load runs over midnight and half the files are off by one day... (3 Replies)
Discussion started by: Calbrenar
3 Replies

6. UNIX for Dummies Questions & Answers

Variable inside command substitution

Hello people. Part of my script: SUBSETID=`echo $PMFILE |sed 's/pmresult_//' | sed 's/_*//'` MAPFILE=`find /huawei/cell /huawei/nodeb /huawei/rnc -name 'mapping_$SUBSETID.txt' |grep -v backup` Unfortunatelly variable $SUBSETID in the MAPFILE declaration will not return the... (3 Replies)
Discussion started by: drbiloukos
3 Replies

7. Shell Programming and Scripting

ksh command substitution not executing block commands

We have a function defined in a ksh script that gets called repeatedly and concurrently by background jobs called from within the same shell script file to run PL/SQL commands against an Oracle database. The function is below: runPLSQL() { ORACLE_SID=${1} procName=${2} #... (7 Replies)
Discussion started by: rjgst5
7 Replies

8. Shell Programming and Scripting

How to use ftp commands inside shell script? Help please

Dears, I'm new in shell scripting and i need your help, i would like to know how can i create a script to ftp to a certain unix/linux machine/server IP address and get a file for example without user intervention? How can i force the script to use a certain username and password to access this... (4 Replies)
Discussion started by: Dendany83
4 Replies

9. Shell Programming and Scripting

Passing UNIX commands inside sed

Hi, In sed, is it possible to match patterns by directly executing UNIX commands inside sed? For e.g. - sed "s/`cat file.txt | cut -d "|" -f2`/replace_string" Will the above command work? My expectation is to search for the second field in file.txt (file delimited by | ) and replace... (10 Replies)
Discussion started by: devanathann
10 Replies

10. Shell Programming and Scripting

String substitution on find results inside exec/xargs

What I'm trying to do is perform a copy, well a ditto actually, on the results of a find command, but some inline string substitution needs to happen. So if I run this code find ./ -name "*.tif" I get back these results. .//1234567.tif .//abcdefg.tif Now the action from exec or xargs I... (2 Replies)
Discussion started by: myndcraft
2 Replies
Login or Register to Ask a Question