SED command help: Can we pass predefined variables in place of regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED command help: Can we pass predefined variables in place of regex
# 1  
Old 03-19-2009
SED command help: Can we pass predefined variables in place of regex

Hi All,
I have a doubt.
Can we assign a regular expression for pattern searching to a variable in a script and then use that variable in place of a regular expression in sed command.I tried but got some syntax error!!Is it not possible.Because my requirement is that i have a generic script to get a threat number from a pstack command of a particulat process id and then i have to seach for that thread in the stack.How can i do it??
Thanks in advance.
# 2  
Old 03-19-2009

Yes, it is possible, but you must enclose the sed command in double quotes not single quotes.
# 3  
Old 03-19-2009
Thanks a lot,Thats where i was making a mistake.
i guess this is the correct syntax:
sed -n "/ $var /p"

with this i will get an output but from that i have to delete any special character that comes for eg:
angle bracket or comma or full stop.
i am using this command but it is not working

sed 's/^( ^, //'

it is showing garbled sed command

could you please help?

my current output is ---> (af43a8,

but my desired output is ---> af43a8

How to get it?
# 4  
Old 03-19-2009
Quote:
Originally Posted by usha rao
Thanks a lot,Thats where i was making a mistake.
i guess this is the correct syntax:
sed -n "/ $var /p"

That depends on the value of $var. What is it?
Quote:
with this i will get an output but from that i have to delete any special character that comes for eg:
angle bracket or comma or full stop.
i am using this command but it is not working

sed 's/^( ^, //'

Code:
sed 's/^[(  ^,]//'

Quote:

it is showing garbled sed command

could you please help?

my current output is ---> (af43a8,

but my desired output is ---> af43a8

How to get it?
# 5  
Old 03-20-2009
I tried tha above code sed 's/^[( ^,]//'
but it is only removing the leading angle bracket how to modify this command so that it can remove trailing comma also??

i have a keyword as (157c328,
with above sed command the output is coming like 157c328,
but i want output as 157c328 i.e without comma,do i have to issue another sed command to remove trailing comma or it can be done only with a single sed command to remove both leading bracket and trailing comma??
Thanks
# 6  
Old 03-20-2009

Code:
sed -e 's/^[(  ^,]//' -e 's/,$//'

# 7  
Old 03-23-2009
I have another problem with SED command.

i have a file which looks like this:
file1{
CPS: Error registering CPS with MBean Server -- this will NOT affect the normal functioning of the program -- CPS w
ill just not be available for dynamic reloading: java.lang.NoClassDefFoundError: javax/management/modelmbean/ModelM
BeanInfo

******* Command Response *******

CMD RESPONSE = tfwk15On:true
tfwk15FullyOn:true
}

here i want to get the final output as true for tfwk15FullyOn

my problem is
i am using a command like this:

cat file1 |sed 's/tfwk15FullyOn:/@/g' |awk -F"@" '{print $2}'| sed '/^$/d' | sed -e 's/^.*{\(.*\)},*/\1/'


with this command i need to get output as true

but instead i am getting as output which looks like

CPS: Error registering CPS with MBean Server -- this will NOT affect the normal functioning of the program -- CPS will just not be available for dynamic reloading: java.lang.NoClassDefFoundError: javax/management/modelmbean/ModelM
BeanInfo
true

whereas i want output as true only...
Is there something wrong with my sed usage??
also here i am using two sed command ,is there any way that it can be done with one sed command??

Thanks in advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help with a sed command involving Regex

Hi, Iam a newbie to SED. I'm faced with a problem as described. Given the file with text 1 rwerwerwe rere 2 fdfefefe fsdfds 3 rerere ffff trtrt 4 aaaa 1234 asadsdsd 5 hfjfjfjsjfsf... (14 Replies)
Discussion started by: SShinde
14 Replies

2. Shell Programming and Scripting

Need to pass value in sed command

Hi, I have a file a.xml containing the below <customerId>000</customerId> and a variable CUSTOMER_ID which is set to '333' Now I want to replace <customerId>000</customerId> with <customerId>333</customerId> by the below sed command and save the output in a file b.xml: sed... (3 Replies)
Discussion started by: RomitaaChawla@1
3 Replies

3. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

4. UNIX for Dummies Questions & Answers

How to pass variables in Expect command?

Hi All, I need to frame a unix script to logon to a unix box. The credentials needs to be obtained from a property file on the same location. I am trying to use 'expect' and 'spawn' command to meet this req. When I am passing values, these commands are working fine. but when I am trying to... (3 Replies)
Discussion started by: mailkarthik
3 Replies

5. Shell Programming and Scripting

Place digit in front of the line searching pattern using sed command

hi All, i want to add the single digit front of the line in the report file and string compare with pattern file. patter file: pattern1.txt pattern num like 4 love 3 john 2 report file: report.txt i like very much but john is good boy i will love u so after execute... (9 Replies)
Discussion started by: krbala1985
9 Replies

6. Shell Programming and Scripting

Variables in SED command

Hi all, I want write a script to display 5rows at times from a input file. my out like: echo " display started" r1 r2 r3 r4 r5 ... Some action items... again i need next 5 lines. can you please advise. (2 Replies)
Discussion started by: koti_rama
2 Replies

7. Shell Programming and Scripting

Place variables at the beginning of each line

Hello all, I am very new to the shell scripting and I hope someone can help me with this. I have thousands of files with certain format of information and I need to do this for all my files. For each file, grab the numbers in the first and second rows and place them in the position 1 and 2... (8 Replies)
Discussion started by: GoldenFire
8 Replies

8. Shell Programming and Scripting

Using SED command in a shell script: Unterminated address regex

Hi All, I am trying to use a sed command in a shell script in order to delete some lines in a file and I got the following error message. I don't understand why it is not working 'cause I have tried with simple quotes, then with double-quotes, and it is not working. sed: -e expression #1,... (7 Replies)
Discussion started by: Alpha3363
7 Replies

9. UNIX for Dummies Questions & Answers

sed command not work with variables?

I am trying to write a simple script which will take a variable with sed to take a line out of a text and display it #!/bin/sh exec 3<list while read list<&3 do echo $list sed -n '$list p'<list2 done this does not work, yet when I replace the $list variable from the sed command and... (1 Reply)
Discussion started by: MaestroRage
1 Replies

10. Shell Programming and Scripting

assigning variables in sed command

I need to assign a variable within a variable in a sed command. I tried doing the following in c shell. set left = 1 set right = 2 set segment = qwerty sed -n -e "/$segment{$left}/,/$segment{$right}/p" file.txt what is wrong with this syntax? (3 Replies)
Discussion started by: wxornot
3 Replies
Login or Register to Ask a Question