LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2012, 02:19 AM   #1
corone
Member
 
Registered: Jul 2009
Posts: 46

Rep: Reputation: 1
a problem with sed when a variable has '/' character.


Hi,
I want to parse and modify a file.

input file:
Code:
$ cat input_file
date : 01/01/2011
output file which I expect:
Code:
$ cat output_file
date : 04/09/2012
A variable NEW_DATE already has a value 04/09/2012.
Code:
NEW_DATE=04/09/2012
I tried the following command.
Code:
$ sed "s/Date :.\+/Date : $NEW_DATE/" input_file > output_file
But,
because the variable NEW_DATE includes '/' characters,
a problem occurs with the above command.

Can this problem be solved?
I cannot modify the value of the variable NEW_DATE.

Thank you.

Last edited by corone; 04-10-2012 at 03:53 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-09-2012, 02:36 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Use a different character for the separator - say "@".
 
2 members found this post helpful.
Old 04-09-2012, 02:37 AM   #3
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
You can possibly get away with using pipe "|" or colon ":" for substitution delimiters with sed.
ie:
Code:
sed "s|foo|bar|g" somefile
 
2 members found this post helpful.
Old 04-09-2012, 03:38 AM   #4
corone
Member
 
Registered: Jul 2009
Posts: 46

Original Poster
Rep: Reputation: 1
Thank you.

Thank you for your answers, syg00 and fukawi1.
 
1 members found this post helpful.
Old 04-09-2012, 07:12 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It seems to me that you may be making it more complicated than necessary. If there's only that single entry, what's keeping you from simply creating the entire output file directly?

Code:
echo "date : $NEW_DATE" >output_file

By the way, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

Last edited by David the H.; 04-09-2012 at 07:15 AM. Reason: minor rewording
 
1 members found this post helpful.
Old 04-09-2012, 11:48 PM   #6
corone
Member
 
Registered: Jul 2009
Posts: 46

Original Poster
Rep: Reputation: 1
Thank you for your reply, David the H.
The given example is much omitted.
Actually, the format file has many entries.

But it is too much omitted.
It's my mistake.
I didn't think that far for the others. Sorry.

Quote:
Originally Posted by David the H. View Post
By the way, environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
Thank you for your tip.
I won't forget the tip.

Last edited by corone; 04-09-2012 at 11:52 PM.
 
Old 04-10-2012, 09:48 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
No problem then. But it does pay sometimes to step back and look at your requirements a different way. Often you find that things don't need to be as complex as you originally believe.

Indeed, you can still use my idea. Just use sed's "c" (change) command instead of "s" (substitute).

Code:
sed -i "/^date/c date : $new_date" file.txt
I've also worked out the commands needed to do it in ed, which is an actual text editor. It's almost identical.

Code:
printf '%s\n' '/^date/c' "date : $new_date" '.' ',p' | ed -s file.txt

printf '%s\n' '/^date/c' "date : $new_date" '.' 'w' | ed -s file.txt
The ",p" command at the end simply prints the changed contents to stdout. Changing it to "w" writes them back to the file.


How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)


Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

Last edited by David the H.; 04-10-2012 at 10:01 AM. Reason: minor typo + added links + switched to printf for command readability
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting nth length of character from a variable to another variable hayloiuy Linux - Newbie 4 05-02-2011 03:47 PM
sed output -> variable problem figo Programming 13 05-31-2009 09:26 AM
[BASH] Hangman Game: changing a character within a variable without use of sed or awk uzi85 Linux - Newbie 1 03-27-2009 01:41 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration