SED:: Using variable while grouping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED:: Using variable while grouping
# 1  
Old 06-11-2010
SED:: Using variable while grouping

Hi,
I have the following script :-

Code:
#!/bin/csh -f
set var="HOST2"
sed -e 's/\(.*TRANSFER TO\).*\(usr\)/\1 "$var" \2/' tempFile

tempFile contains:

Code:
STOP TRANSFER TO HOST1             /usr/bin/myscript

1. How to use variable in the above sed command. It replaces with $var instead of HOST2

2. In the second grouping while replacing, the "/" from usr is gone, how to handle that?

Thanks

Last edited by angshuman_ag; 06-11-2010 at 01:10 PM.. Reason: Typo correction
# 2  
Old 06-11-2010
Try
Code:
sed -e "s/\(.*TRANSFER TO\).*\(\/usr\)/\1 $var \2/"

To "translate" $var into it's value
To also pick the slash "/" from usr
This User Gave Thanks to pseudocoder For This Post:
# 3  
Old 06-11-2010
Thanks AGAIN to you for solving my issue Smilie
# 4  
Old 06-11-2010
No problem. You're welcome. I'm glad it worked Smilie
# 5  
Old 06-28-2010
Hi,
One issue here.
This line

Code:
STOP TRANSFER TO HOST1             /usr/bin/myscript

is like this

Code:
"STOP TRANSFER TO HOST1"             /usr/bin/myscript


So, the sed command removes the closing quotes. How do I preserve that ?
# 6  
Old 06-28-2010
Code:
sed -re "s|(.*TRANSFER TO +).*(\"? +(/usr)|\1$var\2|"

in this case, i use extended regexp (sed -re)
The first set of parenthesis goes from the beginning of the line to TRANSFER TO including following spaces (...TO +).
The second set goes from a possible quote (\"?) to /usr including spaces in between.
Ask me if it's not clear

Last edited by chebarbudo; 06-28-2010 at 07:23 AM.. Reason: using Scrutinizer's good idea (s|...|...| instead of s/.../.../)
# 7  
Old 06-28-2010
Code:
sed "s|\(TRANSFER TO\).*\"|\1 $var\"|"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Name grouping

awk 'FNR==NR {a; next} $NF in a' genes.txt refseq_exons.txt > output.txt I can not figure out how to group the same name in $4 together. Basically, all the SKI together in separate rows and all the TGFB2. Thank you :). chr1 2160133 2161174 SKI chr1 218518675 218520389 TGFB2... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Grouping

Hi all, I am using following command: perl program.pl input.txt output.txt CUTOFF 3 > groups_3.txt containing program.pl, two files (input.txt, output.txt) and getting output in groups_3.txt: But, I wish to have 30 files corresponding to each CUTOFF ranging from 0 to 30 using the same... (1 Reply)
Discussion started by: bioinfo
1 Replies

3. Shell Programming and Scripting

grouping using sed or awk

I have below inside a file. 11.22.33.44 user1 11.22.33.55 user2 I need this manipulated as alias server1.domain.com='ssh user1@11.22.33.44' alias server2.domain.com='ssh user2@11.22.33.55' (3 Replies)
Discussion started by: anil510
3 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

Grouping sed commands

Hello, would you please help me with why my SED command file is outputting the entire input file instead of only the text that I'm trying to block? cat testfile O 111111111-00 DUE-DATE METHOD: FREQUENCY: O 222222222-00 DUE-DATE METHOD: FREQUENCY: O 333333333-02 DUE-DATE METHOD:... (4 Replies)
Discussion started by: lneedh1
4 Replies

6. Shell Programming and Scripting

Expand an environment variable in sed, when the variable contains a slash

I'm trying to make a sed substitution where the substitution pattern is an environment variable to be expanded, but the variable contains a "slash". sed -e 's/<HOME_DIRECTORY>/'$HOME'/'This gives me the following error: sed: -e expression #1, char 21: unknown option to `s'Obviously this is... (2 Replies)
Discussion started by: Ilja
2 Replies

7. Shell Programming and Scripting

Grouping using sed/awk ?

I run awk cat $1|awk '{print $6}' and get a lot of results and I want results to group them. For example my result is (o/p is unknown to user) xyz xyz abc pqr xyz pqr etc I wanna group them as xyz=total found 7 abc=total .... pqr= Thank (3 Replies)
Discussion started by: pujansrt
3 Replies

8. 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

9. Shell Programming and Scripting

gnu sed regex grouping not working?

Hello, from the gnu sed manual, I should be able to do this: `\(REGEXP\)' Groups the inner REGEXP as a whole, this is used to: * Apply postfix operators, like `\(abcd\)*': this will search for zero or more whole sequences of `abcd', while `abcd*' ... (3 Replies)
Discussion started by: Allasso
3 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies
Login or Register to Ask a Question