sed not working while passing the variable to print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed not working while passing the variable to print
# 1  
Old 11-20-2012
sed not working while passing the variable to print

Hi

I have below sed to search and print particular line that matches in file and prints it, which is actually working fine as expected.

Code:
sed -n "/^2012-11-19 23:[0]*/p" filename

but if I replace date with the variable it seems it's not recognizing it and throwing out error

Code:
 v="2012-11-19"
sed -n 's/^'$v' 23:[0]*/p"  filename
 sed -n "s/^$v 23:[0]*/p"  filename

so can anyone help me resolving this??
# 2  
Old 11-20-2012
Quote:
Originally Posted by manas_ranjan
Code:
 v="2012-11-19"
sed -n 's/^'$v' 23:[0]*/p"  filename
 sed -n "s/^$v 23:[0]*/p"  filename


Try

Code:
v="2012-11-19" 
sed -n '/^'"$v"' 23:[0]*/p'  filename


Last edited by pamu; 11-20-2012 at 07:36 AM..
# 3  
Old 11-20-2012
hey pamu,

that was my mistake, actually there was (') instead of (") in my first sed with variable one you pointed out.

thanks for pointing out.

But anywas, I followed your suggestion and tried to execute as below
Code:
sed -n 's/^'"$v"' 23:[0]*/p' filename

then it pops up with
Code:
sed: -e expression #1, char 23: unterminated `s' command

error message
# 4  
Old 11-20-2012
Quote:
Originally Posted by manas_ranjan
Code:
sed -n 's/^'"$v"' 23:[0]*/p' filename

Please look at my post again. You won't find s there.Smilie
And i have highlighted s also in your previous code in red.(check post 2)

pamu
# 5  
Old 11-20-2012
thats so stupid of me, I have no idea why in the first place I even put s.
sorry for this!!

now all good..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing value of a variable in sed

Hi, I want to pass value of a variable track_line which is the line number to sed. Sed should print the lines starting from track_line till the last line of the file. I tried the below command but it is not working. sed -n '${track_line},$p' latest_log_file I tried using the below too but... (1 Reply)
Discussion started by: nitinupadhyaya8
1 Replies

2. Shell Programming and Scripting

Trouble with passing variable to sed

Here is my code #!/bin/bash username=gnowicki sed '$s/$/ $username/' < sshd_config 1 <> sshd_config what this is supposed to do is take the name gnowicki and put it at the end of the last line of the sshd_config and it works except not using the variable, if I put the name "gnowicki" where... (2 Replies)
Discussion started by: slufoot80
2 Replies

3. Shell Programming and Scripting

passing variable to sed command not working

Hello All, I am trying to embed variable in sed command to fetch a portion of record between two pattern. This command is not working ...any suggestion on this how to place the variable in sed command to find a portion . I am using Sun OS (Solaris). Thanks JM (1 Reply)
Discussion started by: jambesh
1 Replies

4. Shell Programming and Scripting

Passing a variable to sed command

Hi guys, I wanted to pass a variable to the sed command which tells which line to be deleted. a=2; echo $a; sed '$ad' c.out it is throwing an error. sed: 0602-403 "$a"d is not a recognized function. I even tried "$a" and \$a.. but it is of no use. Can you please correct me... (6 Replies)
Discussion started by: mac4rfree
6 Replies

5. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

6. Shell Programming and Scripting

Passing a variable to sed or cut

:confused: Is it possible to send a variable to a sed or cut command? I have a test script as below: counter=1 while read line do # Test the file printf "$line" > temp$counter pref=$(cut c1-2000 $temp$counter | sed 's///g' | sed 's|.*PutTime\(.*)Origin.*|\1|') printf" let... (5 Replies)
Discussion started by: gugs
5 Replies

7. Shell Programming and Scripting

Passing a variable to sudo sed command

hi, dataParse(){ line="$@" name="cat /etc/passwd | grep "$line": | cut -f6 -d':'" eval $name > sam.txt 2>&1 sudo -u $line sed -n 's/data-1/&/p' $name/test.xml >> sam1.txt } Here i getting the homedir of the accounts and is set in name variable.which returns "/home/raju" which i... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

8. Shell Programming and Scripting

Passing a variable in SED getting command garbled

I fairly new to SED. I have tried many different variations of this line of code and even breaking it down into its components and running them separately. They work individually without variables but when I place the $todbname variable it will either inserts the text "connect to $todbname"... (3 Replies)
Discussion started by: edewerth
3 Replies

9. Shell Programming and Scripting

variable passing to sed

I m trying to pass variable to sed. export var=140920060731 sed -e '/$var/d' file but no luch so far..? any body has any idea abt it Is there any way to pass variable to SED? Thanks , Manish (2 Replies)
Discussion started by: Manish Jha
2 Replies

10. Shell Programming and Scripting

Passing output of sed/echo to a variable

I understand how to use a variable in a sed command, but for the life of me I can't get the output into a variable. I'm making a general function to replace part of a filename with a different string, so: >>myscript this that would change: this_file001.txt to that_file001.txt and... (11 Replies)
Discussion started by: donflamenco
11 Replies
Login or Register to Ask a Question