How do I use variables in a sed command?


1) Please use


tags around your code and data, to preserve formatting and to improve readability.

2) QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-broken by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

As you can see, the solution to your sed problem also comes from understanding proper quoting. (BTW, re druuna's post, the brackets aren't necessary if you separate the "p" from "counter", as sed ignores spaces between letter commands. You got the error you did because the entire expression needs to be passed to sed as a single unit.)

3) It's recommended to use ((..)) for numerical tests, and [[..]] for...

0 0

4.30. How do I read environment variables with sed?

4.30.1. - on Unix platforms

In Unix, environment variables begin with a dollar sign, such as $TERM, $PATH, $var or $i. In sed, the dollar sign is used to indicate the last line of the input file, the end of a line (in the LHS), or a literal symbol (in the RHS). Sed cannot access variables directly, so one must pay attention to shell quoting requirements to expand the variables properly.

To ALLOW the Unix shell to interpret the dollar sign, put the script in double quotes:

sed "s/_terminal-type_/$TERM/g" input.file >output.file

To PREVENT the Unix shell from interpreting the dollar sign as a shell variable, put the script in single quotes:

sed 's/.$//' infile >outfile

To use BOTH Unix $environment_vars and sed /end-of-line$/ pattern matching, there are two solutions. (1) The easiest is to enclose the script in "double quotes" so the shell can see the $variables, and to prefix the sed...

0 0
0 0
find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

Came across this commandline on which acts as tree command in unix. I want to someone to explain what that sed command is exactly doing. i'm unable to understand the syntax

Right now i know the basic sed command syntax. I'm still getting a hang of the unix environment .

PS: Command works perfectly and The discussion was closed in that forum.

Similar Questions

Every example I was able to find demonstrating the w command of sed has it in the end of the script. What if I can't do that? An example will probably demonstrate the problem better: $ echo '123' | se

In a bash script I've used this method successfully many times: sed -i '/^drop/s/(msg:/(msg:drop - /' /file I think for perl a simple find and replace with sed is this: s/^alert/drop/; How can I

My code to replace all instances of 'foo' with 'bar' : find . -type f | xargs grep 'foo' -l | xargs sed -i 's|foo|bar|g' I'd...

0 0

This is definitely a FAQ. So you have your PATTERN in a shell variable, and it can be an arbitrary string. How to make sure that it is safe to use it as a lhs/rhs/regex in sed?

First, there is a distinction to make. Things are different depending on whether you want to use your variable in the LHS of an s command or in an address regex, or you want to use it in the RHS of a s command. The first case has two sub-cases, respectively when you're using BREs (Basic Regular Expressions, the default in sed) or EREs (Extended Regular Expressions, for those seds that support them like GNU sed). Each case is analyzed separately below.

Use in the LHS or in a regex address

So, you have your variable and want to be able to safely do something like

$ sed "s/${pattern}/FOOBAR/" file.txt # or $ sed "/${pattern}/{do something;}" file.txt # Note double quotes rather than single quotes, so the shell can expand the variables

but of course the risk is that the variable...

0 0
I'm trying to create .remmina profile files for each one of my servers according to my /etc/hosts, i wrote the next script, but my problem is that in the result of the sed command, where i expected to see the value of the $hostname variable instead i see the word $hostname , how can it be done correctly?

#!/bin/bash num=100 for srv in `cat ~/srv.lst`; do cp 1.remmina $num.remmina hostname=grep $srv /etc/hosts | awk -F" " '{print $2}' sed -i 's/name=amavm4/name=$hostname/g' ~/.remmina/$num.remmina sed -i 's/server=amavm4:5906/server=$hostname:5906/g' ~/.remmina/$num.remmina num=$((num+1)) done So after your answers, i've edited the script to look like that:

#!/bin/bash num=1392344753668 for srv in `cat ~/srv.lst`; do cp 1.remmina $num.remmina hostname=`grep $srv /etc/hosts | awk -F" " '{print $2}'` sed -i 's/name=amadw1/name='$hostname'/g' ~/.remmina/$num.remmina sed -i 's/server=amadw1:5906/server='$hostname':5906/g' ~/.remmina/$num.remmina num=$((num+1)) done...

0 0

The shell is responsible for expanding variables. When you use single quotes for strings, its contents will be treated literally, so sed now tries to replace every occurrence of the literal $var1 by ZZ.

Using double quotes

Use double quotes to make the shell expand variables while preserving whitespace:

sed -i "s/$var1/ZZ/g" "$file"

When you require the quote character in the replacement string, you have to precede it with a backslash which will be interpreted by the shell. In the following example, the string quote me will be replaced by "quote me" (the character & is interpreted by sed):

sed -i "s/quote me/\"&\"/" "$file"

Using single quotes

If you've a lot shell meta-characters, consider using single quotes for the pattern, and double quotes for the variable:

sed -i 's,'"$pattern"',Say hurrah to &: \0/,' "$file"

Notice how I use s,pattern,replacement, instead of s/pattern/replacement/, I did it to avoid interference with the / in...

0 0

grep does that job well enough.

$ echo "::=BEGIN" > testfile1.txt $ grep "::=BEGIN" -q testfile1.txt && echo "FOUND" FOUND $ grep "::=BEGIN" -q testfile1.txt && echo "FOUND" || echo "NOTFOUND" FOUND $ grep "whever" -q testfile1.txt && echo "FOUND" || echo "NOTFOUND" NOTFOUND

What the code does is simply running a quiet search within subshell. && , or logical AND operand, checks if the first command succeeded, and if it did, then it runs echo "FOUND" . || checks if that echo has been run, i.e., if we found anything. Second echo runs only if the first command failed.

Here's an awk version:

$ awk '{i="NOTFOUND";if($0~/::=BEGIN/) i="FOUNDIT";exit } END { print i}' testfile1.txt FOUNDIT $ awk '{i="NOTFOUND";if($0~/whatevs/) i="FOUNDIT";exit } END { print i}' testfile1.txt NOTFOUND

Basic idea here is to set i to NOTFOUND, and if we find the string - change it to FOUNDIT . At the end after the first set of has finished processing file, we will print i, and it will...

0 0
find /home/www/ -type f -exec perl -i.bak -pe 's/subdomainA\.example\.com/' {} +

find /home/www/ -type f will list all files in /home/www/ (and its subdirectories). The "-exec" flag tells find to run the following command on each file found.

perl -i.bak -pe 's/subdomainA\.example\.com/' {} +

is the command run on the files (many at a time). The {} gets replaced by file names. The + at the end of the command tells find to build one command for many filenames.

Per the find man page: "The command line is built in much the same way that xargs builds its command lines."

Thus it's possible to achieve your goal (and handle filenames containing spaces) without using xargs -0, or...

0 0

nohup only writes to nohup.out if the output is otherwise to the terminal. If you redirect the output of the command somewhere else - including /dev/null - that's where it goes instead.

nohup command >/dev/null 2>&1 # doesn't create nohup.out

If you're using nohup, that probably means you want to run the command in the background by putting another & on the end of the whole thing:

nohup command >/dev/null 2>&1 & # runs in background, still doesn't create nohup.out

On Linux, running a job with nohup automatically closes its input as well. On other systems, notably BSD and OS X, that is not the case, so when running in the background, you might want to close its input manually. While closing input has no effect on the creation or not of nohup.out, it avoids another problem: if a background process tries to read anything from standard input, it will pause, waiting for you to bring it back to the foreground and type something. So the extra-safe version looks like...

0 0

The filter sed can process text from standard input and write its results to standard output. The input can be redirected from a file, and the output also can be redirected to a file using your shell's redirection capabilities. It has hundreds of uses, and once you learn sed, you really would miss it if you lost it.

sed can append lines, remove lines, change lines, rearrange lines, substitute text strings and more. Using sed, you can write simple scripts that can become powerful text manipulating commands.

sed can use regular expressions to define what processing will occur on lines of text and which lines it processes. If you have never seen or used regular expressions before, you may want to become familiar with the basic syntax of regular expressions. In this article, we use a few regular expressions to make sed do some simple text processing.

sed can be run on the command line as follows:

cat sample.txt | sed -e '1,15d'

You can cat the file...

0 0