awk: replace with script parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: replace with script parameter
# 1  
Old 06-01-2012
awk: replace with script parameter

Hi


var=0001

I want to replace 2nd field of file with variable var in file sample.txt

Please suggest with awk. dont want to use awk -v option.

pseudo code : something like this.

var=0001
awk '{ 12193 /var } {print $0 }' sample.txt
# 2  
Old 06-01-2012
try like this.
Code:
$>cat sample.txt
A B C
D S E
P Q R
$>var=0001
$>awk '$2="'"$var"'"' sample.txt
A 0001 C
D 0001 E
P 0001 R

# 3  
Old 06-02-2012
Code:
awk '{$2=v}1' v="$var" file

These 2 Users Gave Thanks to Scrutinizer For This Post:
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 script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Replace value from input parameter

Hi Guys, I am having a script file where in getting input parameter as string. I need to assign the variable but not able to achieve #!/bin/bash input=$1 replace=string_$input_string2 echo $replace I am getting but should get string_<input_value>_string2 string_ (1 Reply)
Discussion started by: rohit_shinez
1 Replies

3. Shell Programming and Scripting

Replace multiple file by passing parameter value

Hello All, I want to change date part in file name to yesterday date in the file name. example file name file-12122017-06-30-41.dat want file-12112017-06-30-41.dat I am doing like below. Below it is not changing the filename. Actually it is not parsing the $today and $yesterday value in... (1 Reply)
Discussion started by: looney
1 Replies

4. Shell Programming and Scripting

Call Script with Parameter (that has another parameter)

Hi. How do I achieve this sh /EDWH-DMT02/script/MISC/exec_sql.sh "@/EDWH-DMT02/script/others/CSM_CKC/Complete_List.sql ${file_name}" Complete_List.txt The /EDWH-DMT02/script/MISC/exec_sql.sh has two parameters and it's working fine with this sh /EDWH-DMT02/script/MISC/exec_sql.sh... (7 Replies)
Discussion started by: aimy
7 Replies

5. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

6. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

7. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

8. Shell Programming and Scripting

How to extract and replace with parameter value?

Hi, I have a function getparam() which takes parameter name as input and parameter value as output. I have a strting like this, abcd#paramname1#efgh#paramname2#ijklmnopq Above one is my input, The strting enclosed in # are the parameter names. Here question is, I have to get the... (0 Replies)
Discussion started by: anandapani
0 Replies

9. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

10. Shell Programming and Scripting

awk/sed script to read values from parameter files

Hi, I am writing a shell program that executes a lot of Oracle SQL Files on different databases based on the enviroment setting value. I am trying to design a parameter file where i can store the environment values for all the databases in the below format Environment File File Name... (6 Replies)
Discussion started by: rajan_san
6 Replies
Login or Register to Ask a Question