awk search and replace nth column by using a variable.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search and replace nth column by using a variable.
# 1  
Old 12-29-2015
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"

Code:
TempVal=`echo 1000`
Cat file_name.txt | awk -F, -v OFS=, '{$2="\"$TempVal\""; print }

'

After running the above command - Output :

Code:
"1","$TempVal","3"

Moderator's Comments:
Mod Comment This is ridiculous. Using CODE tags correctly is not that hard. Please review this tutorial that you have seen at least 10 times before:
  1. Text explaining your problem does not need to be tagged.
  2. Full-line and multi-line text showing sample input, sample output, or code needs to be enclosed in CODE tags.
  3. Partial-line text showing sample input, sample output, or code can be enclosed in CODE tags or ICODE tags.

Last edited by Don Cragun; 12-29-2015 at 04:19 AM.. Reason: Get rid of extraneous HTML tags, add CODE and ICODE tags.
# 2  
Old 12-29-2015
Couple of things, You don't need to use cat to pass the file content to awk. It's useless use of cat.

You can specify the single quote to get the shell variable interpolate.

Code:
awk -F, '{$2="\"'$TempVal'\""; print }' OFS=, file_name.txt

(Or)

You can use -v option to assign the shell variable data into awk variable and make use of the awk variable inside the awk block.

Code:
awk -F, -v t="\"$TempVal\"" '{$2=t; print }' OFS=, file_name.txt

-Ranga

Last edited by rangarasan; 12-29-2015 at 03:51 AM.. Reason: Format
These 2 Users Gave Thanks to rangarasan For This Post:
# 3  
Old 12-29-2015
In addition to what Ranga has already suggested, you don't need to use command substitution to assign a simple string to a variable. Use:
Code:
TempVal=1000

not:
Code:
TempVal=`echo 1000`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Value of nth Column of Each Line Using Array

Hello All, I am writing a shell script with following requirement: 1. I have one input file as below CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 Replies

2. Shell Programming and Scripting

Search for string in column using variable: awk

I'm interested to match column pattern through awk using an external variable for data: -9 1:751343:T:A -9 0 T A 0.726 -5.408837e-03 9.576603e-03 7.967536e-01 5.722312e-01 -9 1:751756:T:C -9 0 T C 0.727 -5.360458e-03 9.579447e-03 7.966977e-01 5.757858e-01... (7 Replies)
Discussion started by: genome
7 Replies

3. Shell Programming and Scripting

Search term in nth field and replace kth column

Hi, I have a text file which looks like this a.txt A,12,Apple,Red B,33,Banana,Yellow C,66,Sky,Blue I need to search for a particular field(s) in particular column(s) and for that matching line need to replace the nth column. Sample scenario 1: Search for 66 in second field and Sky in... (5 Replies)
Discussion started by: wahi80
5 Replies

4. Shell Programming and Scripting

How to search and replace string from nth column from a file?

I wanted to search for a string and replace it with other string from nth column of a file which is comma seperated which I am able to do with below # For Comma seperated file without quotes awk 'BEGIN{OFS=FS=","}$"'"$ColumnNo"'"=="'"$PPK"'"{$"'"$ColumnNo"'"="'"$NPK"'"}{print}' ${FileName} ... (5 Replies)
Discussion started by: Amit Joshi
5 Replies

5. 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

6. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

7. Shell Programming and Scripting

Replace the nth column date as MM/DD/YYYY

Hi, I need some unix command to replace the following thing. cat test.dat 1234|test|8/19/2009|8/20/2009|test 1234|test|8/9/2009|8/21/2009|test 1234|test|8/1/2009|8/2/2009|test after processing 1234|test|08/19/2009|08/20/2009|test 1234|test|08/09/2009|08/21/2009|test... (6 Replies)
Discussion started by: anshaa
6 Replies

8. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

9. Shell Programming and Scripting

awk/sed to search & replace data in first column

Hi All, I need help in manipulating the data in first column in a file. The sample data looks like below, Mon Jul 18 00:32:52 EDT 2011,NULL,UAT Jul 19 2011,NULL,UAT 1] All field in the file are separated by "," 2] File is having weekly data extracted from database 3] For eg.... (8 Replies)
Discussion started by: gr8_usk
8 Replies

10. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies
Login or Register to Ask a Question