Register
It is currently Sun Dec 21, 2014 7:45 am

sed : Using an output of command as regexp [solved]


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Mon Jan 10, 2011 10:10 am   

Joined: Mon Jan 10, 2011 9:29 am
Posts: 3
Location: Bandung, Indonesia
How to use an output of a command or a variable as regexp or replacement in sed ?

I'd like to use sed at this way :

ls -1 input.txt | sed 's/xxxx-xx-xx/`date +%Y-%m-%d`/g' > output.txt

or

d=`date +%Y-%m-%d`
ls -1 input.txt | sed 's/xxxx-xx-xx/`$d`/g' > output.txt

(where the output of the `date +%Y-%m-%d` command or content of $d variable can be used as replacement or regexp in sed)

But it seems like the sed doesn't recognise the commands..
Is there a way to be done ?

Thanks..


Last edited by mardon86 on Wed Jan 12, 2011 10:25 am, edited 1 time in total.

Top
 Profile YIM  
 PostPosted: Mon Jan 10, 2011 10:35 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 579
hi,

Quote:
it seems like the sed doesn't recognise the commands..

it's because of single quotes.
you may write :
Code:
ls -1 input.txt | sed 's/xxxx-xx-xx/'"$(date +%Y-%m-%d)"'/g' > output.txt
or
Code:
d=$(date +%Y-%m-%d)
ls -1 input.txt | sed "s/xxxx-xx-xx/$d/g" > output.txt


Top
 Profile  
 PostPosted: Mon Jan 10, 2011 7:22 pm   

Joined: Mon Jan 10, 2011 9:29 am
Posts: 3
Location: Bandung, Indonesia
Oh,, they're working,,

but i dont understand why d=$(date +%Y-%m-%d)
i usually type d=`date +%Y-%m-%d` and it works..
and i dont understand too about the usages of such characters like ' " ` ( [ and {

where can i get those information ?

thanks..


Top
 Profile YIM  
 PostPosted: Wed Jan 12, 2011 10:05 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hi Mardon86!

Essentially, $(date +%Y-%m-%d) and `date +%Y-%m-%d` do exactly the same thing. Personally, I find it easier to read when using $(...). It's too easy to mistake the `back-tick` for a 'single-quote (apostrophe)', depending on the font that you're using.

It mostly makes it easier to read, and therefore easier for someone who didn't write the script to understand what's going on.

As for such characters like ' " ` ( [ and {, it really depends where they're being used. The Bash Scripting Guide and the Advanced Bash Scripting Guide at The Linux Documentation Project are great references for learning about those types of things.

I hope this helps!


Top
 Profile YIM  
 PostPosted: Wed Jan 12, 2011 10:21 am   

Joined: Mon Jan 10, 2011 9:29 am
Posts: 3
Location: Bandung, Indonesia
oh,, thanks for helping,, :))

-SOLVED-


Top
 Profile YIM  
 PostPosted: Thu Feb 24, 2011 11:20 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
jeo wrote:
Hi Mardon86!

Essentially, $(date +%Y-%m-%d) and `date +%Y-%m-%d` do exactly the same thing. Personally, I find it easier to read when using $(...). It's too easy to mistake the `back-tick` for a 'single-quote (apostrophe)', depending on the font that you're using.

There is one more important difference. You cannot nest back quotes, but you can nest $()'s:
Code:
TABLE=$(ls $(grep january file-list.txt))


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP