Replace with shell variable in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace with shell variable in awk
# 1  
Old 09-26-2017
Replace with shell variable in awk

Hi,

Probably this is a out of scope of the thread. I have a requirement as below

I have a file with below content

Code:
CMD //DS > testfile

I have to replace
Code:
//DS

with a value which is assigned to a variable outside using awk gsub.
For example
Code:
var=TEST

I expect an output as
Code:
CMD TEST

I tried below 2 awk

Code:
awk '{gsub(/\/\/DS/,$var);print}' testfile

this fails
Code:
awk '{gsub(/\/\/DS/,"$var");print}' testfile

This doesn't replace the var value.

Could you please help me ?

---------- Post updated at 11:52 AM ---------- Previous update was at 11:31 AM ----------

I could accomplish this using sed as below

Code:
sed 's/\/\/DS/,'$var'/g' filename

Still looking for awk

Thanks

Moderator's Comments:
Mod Comment Post was moved to a new thread. Please do not piggyback your questions on an existing thread. Next time start your own..

Last edited by Scrutinizer; 09-26-2017 at 03:10 PM.. Reason: code tags
# 2  
Old 09-26-2017
Try:
Code:
awk -v var="$var" '{gsub(/\/\/DS/,var);print}' testfile

--
Note: The reason your awk attempt did not work is that the single quotes preventes the shell variables from being expanded..

Last edited by Scrutinizer; 09-27-2017 at 02:31 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed to replace variable in file

Hi All I have one file with multiple lines in it, each line has static text and some variable enclosed in <<filename>> as well. e.g. as below 123, <<file1.txt>> this is my name, I stay at <<city.txt>> Thanks for visiting 348384y, this is my name <<fileabc.txt>>, I stay at near the mall of... (8 Replies)
Discussion started by: reldb
8 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

4. Shell Programming and Scripting

How to search, replace and multiply variable within awk?

I have a file that reports the size of disks GB's or TB's - I need the file to report everything in MB's. Here is an extract of the file - the last column is the disk size. 19BC 2363 20G 1AA3 2363 2.93T 1A94 2363 750G Whenever I come across a G I want to delete the G and multiply by... (2 Replies)
Discussion started by: kieranfoley
2 Replies

5. Shell Programming and Scripting

Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition... (3 Replies)
Discussion started by: DerekAsirvadem
3 Replies

6. Shell Programming and Scripting

AWK How to replace a field using 2 shell variables?

Hello everybody: I want to replace any field $2 of any file line (f.i. test.txt) matching $1 with a shell variable. $ cat test.txt F 0 B A H -12.33 Now I'm going to ask the value of variable B: $ SEARCHVAR=B $ OLDVAL=$(awk -v SEARCHVAR="$SEARCHVAR"... (4 Replies)
Discussion started by: basalt
4 Replies

7. Shell Programming and Scripting

Replace a var in sql file with shell script variable

I have a requirement where i have a sql file (filetext.sql). This file contains a variable ss_code. Now in a shell script im trying to replace the variable ss_code with a value contained in the shell script variable MTK_DC..tried the below in the script MTK_DC="mit,cit,bit" OUT=`awk -v... (4 Replies)
Discussion started by: michaelrozar17
4 Replies

8. Shell Programming and Scripting

Find and replace with variable using sed or awk

hi, i have file say email.temp looks like Bell_BB 17 Bell_MONTHLY 888 SOLO_UNBEATABLE 721 and another file r3 Bell BB,Bell_BB Bell,Bell_MONTHLY SOLO,SOLO_UNBEATABLE i want email.temp files $1 say Bell_BB should be replaced by r3 Bell BB and Bell_MONTHLY by... (2 Replies)
Discussion started by: raghavendra.cse
2 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Shell variable with awk

line_no=6 echo 'Phone,' `awk 'NR==$line_no{print;exit}' <filename>` what is the error in this.. it says.. awk: Field $() is not correct. The input line number is 1. The file is <filename>. The source line number is 1. i want to print the data in the $line_no line of a certain... (2 Replies)
Discussion started by: St.Fartatric
2 Replies
Login or Register to Ask a Question