|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
AWK(reading from a variable)
|
Original Message
|
Name: mukrish
Date: August 30, 2005 at 14:31:05 Pacific
Subject: AWK(reading from a variable)OS: Sun SolarisCPU/Ram: Solaris |
Comment: is there a way to replace the nth field in a variable in awk? e.g. given a variable in awk: Var="1,23,3,4" i need to replace the 3rd field(assuming delimiter=comma) to "789" and print the output. so my output should look like "1,23,789,4". i cannot use the traditional match-and-replace commands(they replace based on the value, not the position) as the value of the nth field is not fixed. the other option is getline. but i didn't find any construct to redirect the contents of a variable to getline. any help is appreciated. Tushar.
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: August 31, 2005 at 14:35:58 Pacific
|
Reply: Here's one way, but it's not very nice. Split the variable up into an array and change the required element in the array - 3 in this case. If I were you, I'd consider creating an awk function to perform this: echo " "|awk ' { f=3 # field to change var="1,23,4,4" nvar="" rv=789 nf=split(var,a,",") for(i=1; i<=nf; i++) { if(i == f) a[i]=rv if(i == 1) { nvar=a[i] continue } nvar=nvar","a[i] } var=nvar print var } '
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: mukrish
Date: September 1, 2005 at 06:51:10 Pacific
|
Reply: thanks nails. i know this solution. basically it will work. but for big files, it will take too long. atleast, something is better than nothing. :) for better performance, i was hoping for something like pointing the input-stream to this variable. then i could simply say: "$3=789; print". the reasoning is it will apply the default rules(field-separation, etc) directly on the contents of the variable. but i don't think awk supports it.
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: Luke Chi
Date: September 7, 2005 at 13:39:31 Pacific
|
Reply: VAR="1,23,3,4" echo $VAR | awk -F"," ' { printf("%s,%s,%s,%s,%s,%s\n",$1,$2,"789",$4,$5,$6)} ' | sed 's/,*$//' Luke Chi
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Unix Forum Home
Results for: AWK(reading from a variable)
reading from a file in unix Summary: how to read from a file line by line in unix ... www.computing.net/answers/unix/reading-from-a-file-in-unix/7355.html
Reading from a file Summary: Hi I have a script that reads from a text file and in the script I have conditional statements. The script works if I create a text file from scratch, but for some reason, my conditional statements do... www.computing.net/answers/unix/reading-from-a-file/7007.html
How to remove '$' from a $ variable Summary: I like to know how to remove '$' from a $ variable. I create a script, a.sh as below: #!/usr/bin/ksh a=`echo $1 | cut -d"\$" -f1` b=`echo $1 | cut -d"\$" -f2` echo $a$b However, this script does not w... www.computing.net/answers/unix/how-to-remove-from-a-variable/7936.html
|
|

|