assigning variables in sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assigning variables in sed command
# 1  
Old 02-03-2006
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?
# 2  
Old 02-04-2006
Is the -e really needed? -e is for executing a script.
# 3  
Old 02-04-2006
Quote:
Originally Posted by wxornot
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?
sed goes by
sed 's/<searchtext>/<replacementtext>/' file

using variables within sed

a=abcdef
b=def
echo abcdefghij | sed "s/$b/$a/"
abcabcdefghij

hope this helps.
# 4  
Old 02-04-2006
Quote:
Originally Posted by wxornot
sed -n -e "/$segment{$left}/,/$segment{$right}/p" file.txt
did you mean :
Code:
sed -ne "/$segment${left}/,/$segment${right}/p" file.txt

?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning Variables

Hi, Can the below be clarified please. i just want to know what is the difference between the two ways of assigning variables as mentioned below. export SRC_TBL=${SRC_TBL-"MMA_COPAY_PLN_FACT_STG"} export SRC_TBL="MMA_COPAY_PLN_FACT_STG" thanks in advance :) Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

2. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

3. Shell Programming and Scripting

How to use variables in 'sed' append command?

HELLO!! I'm trying to pass a variable with in the 'sed' command (which would add some piece of code to file at a particular line). We can use sed '{line-number}a\ alfjaljf\ aslfjsfsjafl\ adlfjaf\' file.txt If file.txt is Now, I would like to add the parameter 'lmn' after... (1 Reply)
Discussion started by: mjavalkar
1 Replies

4. Shell Programming and Scripting

Help with reading and assigning variables

Hi Gurus, I have a file named log with 2 lines Each line is a file name. eg $ cat log monday tuesday I need to read log and assign each output(filename) to a different variable. The following doesn't work:- while read A B do echo " a is ${A} " echo " b is ${B} " done <... (6 Replies)
Discussion started by: wisdom
6 Replies

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

6. UNIX for Dummies Questions & Answers

Assigning variables using awk

Hi, I am having a line which is separated by - I need to extract each field and put into some variable. a=`echo "this-is-the-case" | awk -F- '{print $1}' ` b=`echo "this-is-the-case" | awk -F- '{print $2}' ` c=`echo "this-is-the-case" | awk -F- '{print $3}' ` d=`echo "this-is-the-case" | awk... (2 Replies)
Discussion started by: posix
2 Replies

7. Shell Programming and Scripting

variables not assigning in a function

Hi GUYS, I have function. I am assigning a line count to count variable. But it is throwing an error at this line. function_recur (){ #file being created in this function lenth = `wc -l function_outpu.dat`; echo $lenth; } this is the error i got rec.ksh: lenth: not found. ... (3 Replies)
Discussion started by: mac4rfree
3 Replies

8. UNIX for Advanced & Expert Users

assigning variables to their defaults

Hi, Is there any way to assign defaults values to the shell variables without reassigning them ( restarting the session) for example after login the value of ORACLE_HOME=/a/b/c i have changed this value from the console export ORACLE_HOME=/c/d now what if i want the value exported to... (1 Reply)
Discussion started by: clx
1 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. UNIX for Dummies Questions & Answers

assigning variables

Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have Gotopg = "go to page 3" then page = 3 (9 Replies)
Discussion started by: k@ssidy
9 Replies
Login or Register to Ask a Question