Query on using command "sed" for replacing text in bash shell


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Query on using command "sed" for replacing text in bash shell
# 1  
Old 05-28-2019
Query on using command "sed" for replacing text in bash shell

Re: Query on using command "SED" for replacing text in bash shell



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 with variable name "hourid"
Here is what I tried.


Code:
sed -i 's@3@hourid$@g file_name.txt


I want to know how to provide a right syntax for replacing variable "hourid"

Your suggestions are greatly appreciated


Thanks very much!

Last edited by hicksd8; 05-30-2019 at 06:21 AM.. Reason: Fix typo "Querry"
# 2  
Old 05-28-2019
You are using @ as a regex delimiter in sed?

Here is a quick example of how sed is used, in practice:

Code:
sed 's/regexp/replacement/g' inputFileName > outputFileName

# 3  
Old 05-29-2019
Your request is not too clear. Do you want to replace a constant string ("3") in your text with the expanded value of a shell variable ($hourid)? Then, try double quotes, as single quote prevent expansion:
Code:
sed "s@2@${hourid}@g" file

Be careful not to trigger on false positives (e.g. 13, 3567, etc.)
This User Gave Thanks to RudiC For This Post:
# 4  
Old 05-29-2019
@Neo, I am using "@" as a delimiter since the text contains "/"
I assume one can use several delimiters such as @, # !
This User Gave Thanks to achandra81 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

"Command not found" doing a while loop in bash/shell

i=0 numberofproducts=${#urls} #gets number of entries in array called "urls" numberofproductsminusone=`expr $numberofproducts - 1` #-subtract by one while do wget ${urls} i=$(( $i + 1 )) sleep 10 done I'm getting an error ./scrape: line 22: [0: command not found that... (3 Replies)
Discussion started by: phpchick
3 Replies

3. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Simplify Bash Script Using "sed" Or "awk"

Input file: 2 aux003.net3.com error12 6 awn0117.net1.com error13 84 aux008 error14 29 aux001.ha.ux.isd.com error12 209 aux002.vm.ux.isd.com error34 21 alx0027.vm.net2.com error12 227 dux001.net5.com error123 22 us008.dot.net2.com error121 13 us009.net2.com error129Expected Output: 2... (4 Replies)
Discussion started by: sQew
4 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

"sed" command is not working in shell script

Hi All, I am not much strong in shell scripting... I am using sed command in my script to find and replace a string....... This is how script looks : ############# #!/usr/bin/ksh CONFIG_FILE=iom_test.txt FIND=`echo "NIS_FTP_SERVER1=123.456.iom.com"` REPLACE=`echo... (2 Replies)
Discussion started by: askumarece
2 Replies

10. Shell Programming and Scripting

Help with replacing tabs inside "" with some text/blank

I am poor with scripting;) I have a file in the following format; 'This is a "test in production" of importance.' I want to get rid of the spaces inside the "" part only to get the output as, 'This is a "testinproduction" of importance.' (1 Reply)
Discussion started by: shmathew
1 Replies
Login or Register to Ask a Question