Issue in using variable within sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue in using variable within sed command
# 1  
Old 06-05-2012
Issue in using variable within sed command

Hi All,

I am trying to use a variable within the sed command but I am not able to get the output.

When I am using the following command (without variable) its working fine:
Code:
sed -n '/2011\/12\/10 18:11:11./,$p' < Log.txt > Delta_Log.txt

But when I am putting the value 2011\/12\/10 18:11:11. in variable date_str and then use the variable, there is no output.

Have tried the following syntax's:
Code:
sed -n '/$date_str/,$p' < Log.txt > Delta_Log.txt

Code:
sed -n '/"$date_str"/,$p' < Log.txt > Delta_Log.txt


Last edited by Franklin52; 06-05-2012 at 09:10 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 06-05-2012
Single quotes prevent the shell to expand the variables, try it with double quotes:
Code:
sed -n "/$date_str/,$p" < Log.txt > Delta_Log.txt

# 3  
Old 06-05-2012
Hi Franklin,

Tried it with double quotes but getting the following error message:

Code:
sed: Function /2011\/12\/10 18:11:11./, cannot be parsed


Last edited by Scrutinizer; 06-05-2012 at 09:18 AM.. Reason: code tags
# 4  
Old 06-05-2012
When you are weak-quoting (double-quotes) for expansion/evaluation by the shell, make sure that the $ (last line for sed) is not evaluated by escaping it.

Code:
sed -n "/${date_str}/,\$p" <filename>

# 5  
Old 06-05-2012
Thanks a lot!!!

Now it works
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with sed command

Hi, I have a script that replaces one string with the other in all files under the specified directory. !/bin/bash # **************** Change Variables Here ************ startdirectory="/opt/app/properties/tmp_new" searchterm="Oracle/Middleware/" replaceterm="" #... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. UNIX for Dummies Questions & Answers

Issue with sed command

Hi, I want to use a code like sed -n "1,9988p" filename | wc -l I tried from=1 till=9988 sed -n "/$from/,/$till/p" filename |wc -l Issue: sed -n "1,9988p" filename | wc -l -- Returns 9988 rows But sed -n "/$from/,/$till/p" filename |wc -l # Returns > 9988 rows(Complete file) I... (2 Replies)
Discussion started by: Gurkamal83
2 Replies

3. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

4. Shell Programming and Scripting

Issue with SED command

I have a file that contains lines like this: allgroups: cn=Role1,cn=groups,o=xyz,st=mn,c=us allgroups: cn=Role1,cn=groups,o=xyz,st=mn,c=us I want to remove the string : ,cn=groups,o=xyz,st=mn,c=us and so I tried to use SED. i tried to assign it to a variable and use it: ... (3 Replies)
Discussion started by: vskr72
3 Replies

5. Shell Programming and Scripting

Issue with Sed command

I need to search for a keyword UTF-16 in a list of files if that keyword is found then i need to convert the file to UTF-8 format using iconv command. After this i should substitute the UTF-16 keyword inside the file to UTF-8. Please suggest how to do this in shell scripting. (11 Replies)
Discussion started by: Shruthi8818
11 Replies

6. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

7. Shell Programming and Scripting

sed command issue

Hi everybody, I have come across a typical problem: I need to use sed command to replace an apostrophe but it is saying no match found in the error sed -e 's/`/'/g' ...but it is not working. Can you please tell me how to use this apostrophe in this sed command please. Thanks. Rubin (8 Replies)
Discussion started by: RubinPat
8 Replies

8. UNIX Desktop Questions & Answers

Issue with sed command

Hi, I have to replace the character AL2 with AL16 which I have in one of my text file . I used the sed command like this sed 's/sed 's/AL2/AL16/g' test_carr.dat>test_carr1.dat But it is not replacing this value to the output file. The test_carr.dat file contains data like this cat... (1 Reply)
Discussion started by: kavithakuttyk
1 Replies

9. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

10. Shell Programming and Scripting

sed variable issue

I'm writting a krn shell script and i'm having an issue with a variable using sed. If someone enters a variable with a " /" the sed command doesn't read it right. example. a='12000/10-DC' b='12000/10-AC' cat file | sed "s/$a/$b/" How can i correct this issue? Will the fix to this... (2 Replies)
Discussion started by: wisher115
2 Replies
Login or Register to Ask a Question