AWK: replace single positional character given variables
I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk.
Story: I'm automating the (re)configuration of network interfaces, including hard-coding of speed/duplex, using the /kernel/drv/*.conf files, through an interactive script. The script asks the user which interface needs to be configured. If the user enters e1000g*, then the script gets the interface instance from that input by simply awking the echo of the variable and using the g as the field delimiter. e1000g interfaces are hard-coded using the /kernel/drv/e1000g.conf file. In this file, the default line that sets all e1000g interfaces sets them all to autoneg, and shows as:
Each number 7 represents an interface from e1000g0 to e1000g15.
So, if for example, the user chooses to set e1000g2 to 100 full duplex, the 3rd 7 in the string needs to be changed to 4, which would hardcode 100 full duplex. The setting is going to be either 7(autoneg for gig interfaces) or 4 (100 full duplex) in all cases
My solution was to simply create an array with the 16 7s, then depending on the variables I get for speed and interface instance, if needed, change that array member to 4, then use the array in a sed command to replace the string of sevens to the string with the new setting. So it looks something like this (assume user entered e1000g2 as INPUT1 and 100 as INPUT2) :
This works just fine, but the loooooong sed command is killing me .
I would like to find a way to contract that line.. I looked into awk, but I just don't "see" the solution.
So, if anyone can come up with a terser command...
something like this? It will need to get 'massaged' to fit the wrapper script, but it's a start.
idx - index into array of 7-s (0-based - adding 1 as awk's arrays are 1-based)
val - value to assign to indexed cell.
something like this? It will need to get 'massaged' to fit the wrapper script, but it's a start.
idx - index into array of 7-s (0-based - adding 1 as awk's arrays are 1-based)
val - value to assign to indexed cell.
I like that. Thanks. I am not that great at awk, and I just couldn't visualize it.
Hi,
Is there a special positional variables for when using the dot (.)?
Scripts are as below:
$: head -100 x.ksh /tmp/y.ksh
==> x.ksh <==
#!/bin/ksh
#
. /tmp/y.ksh 1234 abcd
echo "yvar1 = $yvar1"
echo "yvar2 = $yvar2"
==> /tmp/y.ksh <==
#!/bin/ksh (2 Replies)
Hi,
I have to use the vaious kind of filters based on various fields in the input file like - count occurence of cases where "TRK-GRP" = 169 or like "ADDR-DIG" = 80080.
I don;t know the positional variable for all below fields. Please help.
Input File :
+++ BEST 12-05-27 15:06:49 MDI 3478... (2 Replies)
Hello, my problem is simple & I searched a lot but I couldn't find anything about it:
Basically I'd like to pass $i to a variable, $i being the positional variable; but it is unknown in the beginning so I can't do it like eg. myvar=$3, it HAS to be the "i"..
First, I tried myvar=$($i) ... (8 Replies)
Hi
Does anyone know how to set any character as the field separator with awk/nawk on a solaris 10 box. I have tried using /./ regex but this doesnt work either and im out of ideas.
thanks (7 Replies)
Hi all,
Greetings,
I have the following scenario, The contents of main file are like :
Unix|||||forum|||||||||||||||is||||||the||best
so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it
And i need the output in the following form:
Unix=forum=is=the=best
so=be=on=it
... (3 Replies)
How do you print out a single quote character in AWK? Using the escape character does not seem to work.
{printf "%1$s %2$s%3$s%2$s\n" , "INCLUDE", " \' ", "THIS" }
does not work. Any suggestions? (6 Replies)
Hello,
I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile.
I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
meas is a shell variable, and this works perfectly fine for me:
awk -v var=$meas -F, '$1==var' /abcd/efgh.txt > temp1.csv
However, i want to introduce another shell variable, named, defnfile in the statement, in place of hardcoded path for efgh.txt like:
awk -v var=$meas -F, '$1==var'... (3 Replies)
Hi,
I have a file 'imei_01.txt' having the following contents:
$ cat imei_01.txt
a123456
bbr22135
yet223
where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456')
I am using the following... (5 Replies)