Linked Questions

11
votes
2answers
54k views

Passing a variable to sed [duplicate]

I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script: cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt The result in shared2.txt looks ...
2
votes
2answers
8k views

Why does a sed command work interactively but not in my script? [duplicate]

SED command not replacing in bash under Debian, but works in command line. Works in command line: sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml Not working in ...
1
vote
1answer
900 views

How to replace a word in a file by a word get by read function? [duplicate]

Situation: echo "tell me a word" read the_word How to replace all WordToReplace in a file by the_word? sed does not seems to appreciate: sed -i 's/WordToReplace/$the_word/g' thefile.sh
0
votes
1answer
2k views

sed replace with variable [duplicate]

I am trying to replace matching string with other string. This is what I have ZKHOSTS="host1,host2,host3" old_string="tsd.storage.hbase.zk_quorum*" new_string="tsd.storage.hbase.zk_quorum = $ZKHOSTS" ...
2
votes
2answers
312 views

Using sed with an integer variable [duplicate]

I want to sed using a variable. For example, sed -n '1,10p' file. So, instead of 10, I want to use $k where k is an integer.
0
votes
1answer
417 views

sed replace with value from an argument [duplicate]

Although this seems like a simple task, the sed call is not substituting the value that I send for IP. Instead, it is replacing localhost with "${IP}". substitute() { export IP=$1 echo $IP ...
0
votes
1answer
414 views

Using sed to find and change information in configuration file [duplicate]

There is a file named partner like: abc def ghi There is another conf file like: part=abc var=x var=y id=123 part=def var=z id=345 and so on... I am making a shellscript which reads line from '...
1
vote
2answers
129 views

Sed command runs with hardcoded value in regex but fails with variable in script [duplicate]

My file data Pattern is below and i need output as 6 or 3 or 8 or 4 based on the variable value which is eth0 and eth1 eth0RX:6:eth0TX:3|eth1RX:8:eth1TX:4| Below code works well sed 's/.*eth0RX:\([...
-1
votes
2answers
346 views

appending into middle of a string using sed [duplicate]

I am completely new to sed script. I want to make a script so that it asks user to enter value to be inserted in file. I am not able to find the correct code so the line I have in the text file looks ...
0
votes
2answers
153 views

content from pwd and which failed with sed to be replaced [duplicate]

I have a file which has the following content: BWA='/software/bwa/bwa-0.7.12/bwa' SAMTOOLS='/software/samtools/samtools-1.3.1/samtools' The above tools are on my computer: which bwa => /work/...
0
votes
0answers
294 views

Sed is not replacing with the value of a variable [duplicate]

I'm using sed to modify a file. In simple terms, my modifier looks as so sed -e '11s/$/ $LinuxUsername/' This is suppose to add in their linux username at the end of line 11, however, it is instead ...
1
vote
1answer
143 views

Replacing string in a file with script [duplicate]

One of the ways replacing a string with sed can be done as follows: sed -i 's/old_str/new_str/' file.txt However if replace.sh is sed -i 's/$1/$2/' $3 the command ./replace.sh old_str new_str ...
0
votes
0answers
165 views

Use variable inside sed command [duplicate]

how can i use variables inside this sed command? sed -i.bak -e '/triggerword/,${w $DIR/tempfile' -e 'd;}' $DIR/targetfile inside -e '{w $DIR/tempfile' -e 'd;}' the $DIR/tempfile isn't recognized
2
votes
1answer
94 views

put $LINE value instead of “$LINE” [duplicate]

I have a loop in the script below. It is not working as I want. I need to put the $LINE value in front of each column. While it puts "$LINE" instead of it is value: cat list | while read LINE ; do ...
0
votes
1answer
118 views

replace word with a variable in line start with [duplicate]

I need to know how to replace this line in fstab: tmpfs /dev/shm tmpfs defaults 0 0 with: tmpfs /dev/shm tmpfs defaults,...
0
votes
2answers
50 views

make use of do loop for a variable specified in sed command [duplicate]

I have the following structure of data HG2 MET HG1 THR HG2 THR HG2 GLN The desire output would be HG1 MET HG1 THR HG2 THR HG1 GLN somehow I need to do the following for i in MET GLN do sed 's/...
-3
votes
1answer
43 views

Script to change $HOME in path to tilde ~ [duplicate]

I tried: $ echo $HOME | sed 's/$HOME/~/g but this didn't work, perhaps because of forward slashes? Does anyone have a script to do this either or: using bash builtins using sed Thank you
0
votes
1answer
35 views

How to insert comment at specific variable line number by sed command? [duplicate]

I want to comment out a line (add a # to the beginning). I used this command: sed -i '96s/^/#&/' file.txt on Linux to insert comment at line # 96. If the line number is a variable, then how can I ...
1
vote
0answers
31 views

sed replacing text in a file [duplicate]

I would like to replace <Path_To_BOOST_LIB_DIR> in a file with the content of ${PREFIX}/lib but this command sed -i .bak 's|<Path_To_BOOST_LIB_DIR>|echo ${PREFIX}/lib|' src/CMakeLists....
-1
votes
1answer
24 views

Querry on using command “SED” for replacing text in bash shell [duplicate]

While using the command "sed" for find and replace, I wanted to know how one could find a constant and replace it with a variable inside the quotation syntax of sed? I wanted to replace constant 3 ...
0
votes
0answers
17 views

remove pattern from string using sed and a variable [duplicate]

I want to remove a pattern from a string (using sed and pipes), but this pattern is not always the same and is defined previously on a variable. this example mimics the desired behavior toremove="...
4
votes
3answers
4k views

Problem with passing a variable to sed

I know that when passing a variable to sed, one should use double quote. So when I was using sed to display a line of a file given a line number: sed '894!d' cloud.cpp It works, but since I also ...
2
votes
2answers
2k views

how to extract just a specific part of a file name in bash [duplicate]

I have the following list of files: lab-1:/tmp/tmp.NMFaDO# ls webserver.123.net.1970-01-01.csv ...
4
votes
2answers
8k views

Use a parameter in a command argument

I run a find command to search files with a name containing perl: find /tmp -name '*perl*' /tmp/perl.pl /tmp/run-perl-stage.pl But when I set the perl name in a variable PARAMETER and run the find ...
3
votes
1answer
687 views

grab from a text file a range using two variables as the beginning and end parameters

I'm trying to grab a specific range of lines from a text file using grep; the line range needs to be identified by two variables. so far, I've been trying to do it using the wildcard '*' examples of ...
0
votes
1answer
486 views

How to pass variable in pattern while using sed command?

I have file abc.sh search_dir='dummy' filename='numbers.txt' for entry in "$search_dir"/* do while read p; do sed -i '' "/$p/d" $entry done < $filename done Trying to delete a line ...
1
vote
1answer
336 views

How to change special character in linux using variable [closed]

I am using this sed command in particular file, but it doesn't work. e.g. a="adsfkndsa%/{}==admfkdsa" b=sadkfjdsaklfj sed -i -e 's/$a/$b/g' /tmp/sample.txt Please suggest another way.
-2
votes
4answers
111 views

Json. How to add a sentence after :

The following is an extroct from a json file, that I have: }, { "name": "asdasd", "script": "dsad.js", "merge_logs": ture, "error_file": "/ofghfghgfh.log", "out_file": "/...
3
votes
1answer
156 views

How to use variable in sed command

I am trying to delete two lines ( 7 and 8) from a .txt file. For this I am using following code:- #!/bin/sh Column="7" sed '"$Column",8d' myfile.txt > result.txt on running this script, I am ...
2
votes
1answer
380 views

extract fasta entries from list using while read

I have 28 files that each have ~14,000 "entries". A single entry consists of a header, denoted by >string, a newline, and then a sequence which is a string. Each entry has variable length sequence/...
0
votes
1answer
59 views

I am not able to update a variable through sed command

I have assigned two variables but not able to update x=$(cat /scratch/env.properties | grep ConfigPath) y=$(ConfigPath=/scratch/a/b) then sed to replace ConfigPath in env.properties sed 's/$x/$y/' ...
0
votes
0answers
31 views

How to use sed to replace variable value with a variable of the same name?

I'm trying to write a script that updates a server config file to the latest format. The purpose of the script is to copy over existing (defined) variables to the latest config (in a temp file), then ...