Embed cmd into sed replacement to have dynamic substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Embed cmd into sed replacement to have dynamic substitution
# 1  
Old 10-02-2012
Embed cmd into sed replacement to have dynamic substitution

Hello,
Is it possible to embed the command output to the replacement of sed? Say I need to replace all the blank lines with random strings:
input:
Code:
ACCTTCGTCTTCTGG

GCTTGAGATGGTCCA

GCAGGGCTAGTGACG

GACGAGTCTCTTGAC

ACCAAATCAAAGATC

and output is:
Code:
>26aa36d934d44f06d15b3aab4645a602 $(date | md5sum)
ACCTTCGTCTTCTGG
>0b23ffccf2d708b8b3f0eaed1fbaeae7 
GCTTGAGATGGTCCA
>f7d0caf30e50bbf55f69678854b610cd
GCAGGGCTAGTGACG
>06a28f5fcc2e8c5d984cf2dd8bbd3a61
GACGAGTCTCTTGAC
>f47cd7fa172ab69142f76c6de7ea1c4f
ACCAAATCAAAGATC

something like:
Code:
sed 's/^$/'">$(date | md5sum)"'/g'

but above script print out single same string for each substitution, not as unique different string each time. i.e.
Code:
date | md5sum

is not dynamic. Any help? Thanks a lot!
Yifang

Last edited by yifangt; 10-02-2012 at 06:24 PM..
# 2  
Old 10-02-2012
Code:
while read l ; do [[ -z "$l" ]] && l=">`echo $RANDOM|md5sum|sed 's/ .*//'`"; echo "$l" ; done < infile

# 3  
Old 10-02-2012
RE

Worked like a chime!
The script is twisty to me. Could you explain the do part?
Code:
do [[ -z "$l" ]] && l=">`echo $RANDOM|md5sum|sed 's/ .*//'`";

Wait a minute, the replacement is not unique. Believe it is related to $RANDOM|md5sum. Any clue about this?
Thanks a lot!

Last edited by yifangt; 10-02-2012 at 07:59 PM.. Reason: update
# 4  
Old 10-02-2012
Code:
do [[ -z "$l" ]] && l=">`echo $RANDOM|md5sum|sed 's/ .*//'`";

if line ("$l") is empty replace with ">" plus random generated string (trailing space and other charachters removed).
# 5  
Old 10-03-2012
Explanation

Thanks! Now I understand the replacement part. Can I ask what this part does?
Code:
 [[ -z "$l" ]]

and How come you used && condition to check empty line. Still could not figure it out. Thanks again.
# 6  
Old 10-03-2012
&& : execute following statement if condition is true
This User Gave Thanks to rdrtx1 For This Post:
# 7  
Old 10-03-2012
Quote:
Originally Posted by yifangt
Thanks! Now I understand the replacement part. Can I ask what this part does?
Code:
 [[ -z "$l" ]]

-z means "if string is zero length".
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed replacement inplace

I need to make permanent changes in the file after find and replace using sed. for this i am using sed -i However this is not working. says sed: illegal option -- i I am working on Sun Solaris uname -a SunOS aspsun14 5.10 Generic_150400-13 sun4u sparc SUNW,SPARC-Enterprise any other work... (3 Replies)
Discussion started by: gotamp
3 Replies

2. Shell Programming and Scripting

Incomplete replacement/substitution awk

Hi! (I'm just a newbie in awk & bash scripts) I have a script which replaces one column from an input file with a specified one from the same file. The input and the desired output files are shown below. ~ cat input.file random text Fe 1.33 23.23 3.33 C 21.03 23.23 3.33 Cu 0.00 ... (2 Replies)
Discussion started by: radudownload
2 Replies

3. Shell Programming and Scripting

SED replacement of placeholders

Hi All, I am facing a problem while using SED in Linux. I have a property file which contains a string local.mds.dir=${basedir}/deployCompositesIt has be to replaced with another string, and value of that string should be initialized at runtime. So I use placeholder there. My substituted... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

4. Shell Programming and Scripting

SED replacement

Hi, i have a file with lines, file.txt ------- test is fun testing is better I need to replace 'test' to 'develop' and i used, a=test b=develop sed "s,$a,$b,g" -------- but i see the word 'testing' is also replaced. Need some solution. Is there any way i could replace only 'test' ? (4 Replies)
Discussion started by: giri_luck
4 Replies

5. Shell Programming and Scripting

Replacement with sed

I am trying to replace the line which has string "tablespace" not case senstive.... with below simple script: mysrcipt.sh sed "s/.*/TABLESPACE USERS/g" create_table > tmp mv tmp create_table Is there any better way to do it? If Search string tooooooo long it will be tough to code in... (4 Replies)
Discussion started by: ganeshd
4 Replies

6. Shell Programming and Scripting

Help with sed replacement

This seems like it should be an easy problem, but I'm a noob and I can't figure it out. I'm trying to use sed, but would be happy to use anything that does the job. I am trying to trim off a fixed number of unknown characters from 2 different : delimited fields while keeping the intervening... (4 Replies)
Discussion started by: helix_w
4 Replies

7. UNIX for Dummies Questions & Answers

i am not getting any idea how to do the following with SED cmd

hi friends i am not getting any idea about how to implement the following with the help of SED cmd 1)need a sed command that deletes the first character in each line in a file 2)need sed command that deletes the character before the last character in each line in a file ... (7 Replies)
Discussion started by: sankar_vitam
7 Replies

8. UNIX for Dummies Questions & Answers

sed cmd

write the sed command for swapping the first and 2nd (fields)words in the following file input file cse1 rama 1223 cse2 raju 2453 cse3 sita 3523 i tried with this $sed 's/ \(*\)/ \(*\)/ \2,\1' myfile1 but not getting th required... (4 Replies)
Discussion started by: sankar_vitam
4 Replies

9. Shell Programming and Scripting

Need Replacement for sed

Hi Can anyone provide me the replacement of sed with xargs perl syntax for the below sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' This should be without looping has to take minimal time for search (0 Replies)
Discussion started by: dbsurf
0 Replies

10. UNIX for Dummies Questions & Answers

Replacement using sed

Hi I have the following file that i need to run a sed command on 1<tab>running 2<tab>running 3<tab>running 4<tab>running I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i... (5 Replies)
Discussion started by: handak9
5 Replies
Login or Register to Ask a Question