Sponsored Content
Top Forums Shell Programming and Scripting How can I use a variable in sed? Post 51669 by Perderabo on Thursday 27th of May 2004 04:15:15 PM
Old 05-27-2004
For starters, you can switch to double quotes:

sed "s/mytext/$var/"

Sometimes that does not work. It would depend on the exact value of mytext. So another solution is to turn the single quoted string into two single quoted strings:

sed 's/mytext/'$var'/'

In this case, the 2nd single quoted string is '/'. Just using a backslash would work to that and it saves a character:

sed 's/mytext/'$var\/

There is actually no reason to quote a slash so this should also work with most shells:

sed 's/mytext/'$var/

And there may be no reason to quote anything at all, but again, the actual vakue of mytext determines this. So this may work:

sed s/mytext/$var/

Opps...you only wanted a solution, so I'll quit now....Smilie
These 3 Users Gave Thanks to Perderabo For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Doing a sed on a variable help !

Hi, So what I'm trying to do is I have these variables and if they have a dot in them , I want everyting before and including the dot removed. Any ideas ? Something like this I would want : $var = $var | sed 's/.*\.//' but that does't work. I want to save that removal $var... (2 Replies)
Discussion started by: seaten
2 Replies

2. Shell Programming and Scripting

sed (variable)

HOw can I use any variable in sed command. For example I am using 'sed -e 's/?/$ORACLE_HOME/g' $file_name Here it replaces ? with $ORACLE_HOME. Instead of it I need actual value of $ORACLE_HOME. How can I do that? Please advice. Thanks in advance. Malay (13 Replies)
Discussion started by: malaymaru
13 Replies

3. Shell Programming and Scripting

sed with a variable

Hi, My shell script searches a VALUE in a file, copies it to a variable and updates a line in another file with this new VALUE (replacing the old) The value has a pattern- VALUE=`$$MyDate=11-11-2008 09.09.56.123456` (yes the $ - . = and space are all part of the string) I am having... (6 Replies)
Discussion started by: ngagemaniac
6 Replies

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

5. Shell Programming and Scripting

using variable in sed

i need to use a value in the Variable to print a particular line from a file using sed command. i tried the below one but its is not working sed -n ' "$var"p ' abc.txt but its is not working please help me to sort out this. (3 Replies)
Discussion started by: Kochu77
3 Replies

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

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

8. Shell Programming and Scripting

sed with variable

Hi Friends in sed whether we can use variable.like the following expression. sed -i 's/ABC/$var/g' filename I am using Kernel 2.6.18-194.11.1.el5 RedHat linux. I have tried sed -i 's/ABC/"$var"/g' filename, still not working.:( Please help. Thanks in advance Joy (2 Replies)
Discussion started by: itsjoy2u
2 Replies

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

10. Shell Programming and Scripting

How to use the variable in sed?

version=git release=r8 echo lp-testsuite-git-r8.x86_64.rpm |sed -e "s/-$version-$release.*//g" I want to get lp-testsuite what's wrong with me ? (1 Reply)
Discussion started by: yanglei_fage
1 Replies
troff(1)							   User Commands							  troff(1)

NAME
troff - typeset or format documents SYNOPSIS
troff [-a] [-f] [-Fdir] [-i] [-mname] [-nN] [-olist] [-raN] [-sN] [-Tdest] [-uN] [-z] [filename...] DESCRIPTION
troff formats text in the filenames for typesetting or laser printing. Input to troff is expected to consist of text interspersed with for- matting requests and macros. If no filename argument is present, troff reads standard input. A minus sign (-) as a filename indicates that standard input should be read at that point in the list of input files. The output of troff is usually piped through dpost(1) to create a printable postscript file (see EXAMPLES). OPTIONS
The following options are supported. They may appear in any order, but all must appear before the first filename. -a Send an ASCII approximation of formatted output to standard output. (Note: a rough ASCII version can also be printed out on ordinary terminals with an old and rarely used command, /usr/bin/ta.) -f Do not print a trailer after the final page of output or cause the postprocessor to relinquish control of the device. -Fdir Search directory dir for font width or terminal tables instead of the system default directory. -i Read standard input after all input files are exhausted. -mname Prepend the macro file /usr/share/lib/tmac/name to the input filenames. Note: most references to macro packages include the leading m as part of the name; for example, the man(5) macros reside in /usr/share/lib/tmac/an. The macro directory can be changed by setting the TROFFMACS environment variable to a specific path. Be certain to include the trailing '/' (slash) at the end of the path. -nN Number the first generated page N. -olist Print only pages whose page numbers appear in the comma-separated list of numbers and ranges. A range N-M means pages N through M; an initial -N means from the beginning to page N; and a final N- means from N to the end. -q Quiet mode in nroff; ignored in troff. -raN Set register a (one-character names only) to N. -sN Stop the phototypesetter every N pages. On some devices, troff produces a trailer so you can change cassettes; resume by pressing the typesetter's start button. -Tdest Prepare output for typesetter dest. The following values can be supplied for dest: post A PostScript printer; this is the default value. The output of the -T option must go through dpost(1) before it is sent to a PostScript printer to obtain the proper output. aps Autologic APS-5. -uN Set the emboldening factor for the font mounted in position 3 to N. If N is missing, then set the emboldening factor to 0. -z Suppress formatted output. Only diagnostic messages and messages output using the .tm request are output. OPERANDS
The following operand is supported: filename The file containing text to be processed by troff. EXAMPLES
Example 1: Using troff The following example shows how to print an input text file mytext, coded with formatting requests and macros. The input file contains equations and tables and must go through the tbl(1) and eqn(1) preprocessors before it is formatted by troff with ms macros, processed by dpost(1), and printed by lp(1): tbl mytext | eqn | troff -ms | dpost | lp FILES
/tmp/trtmp temporary file /usr/share/lib/tmac/* standard macro files /usr/lib/font/* font width tables for alternate mounted troff fonts /usr/share/lib/nterm/* terminal driving tables for nroff ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWdoc | +-----------------------------+-----------------------------+ SEE ALSO
checknr(1), col(1), dpost(1), eqn(1), lp(1), man(1), nroff( 1), tbl(1), attributes(5), man(5), me(5), ms(5) NOTES
troff is not 8-bit clean because it is by design based on 7-bit ASCII. Previous documentation incorrectly described the numeric register yr as being the "Last two digits of current year". yr is in actuality the number of years since 1900. To correctly obtain the last two digits of the current year through the year 2099, the definition given below of string register yy may be included in a document and subsequently used to display a two-digit year. Note that any other available one- or two-character register name may be substituted for yy. ." definition of new string register yy--last two digits of year ." use yr (# of years since 1900) if it is < 100 .ie (yr<100 .ds yy (yr .el { ." else, subtract 100 from yr, store in ny .nr ny (yr-100 .ie (ny>9 { ." use ny if it is two digits .ds yy (ny ." remove temporary number register ny .rr ny } .el {.ds yy 0 ." if ny is one digit, append it to 0 .as yy (ny .rr ny } } SunOS 5.10 22 Jul 1998 troff(1)
All times are GMT -4. The time now is 06:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy