Visit Our UNIX and Linux User Community

AWK: replace single positional character given variables

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: replace single positional character given variables
# 1  
Old 05-07-2009
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) :
#Declare the array at the beginning of the script
set -A INT 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

# Next code is all in a function that does a few more things including input 
# checks

if (( ${INPUT2} < 101 )); then
   NIC=$(echo ${INPUT1} | awk -F"g" '{print $2}')
   sed "s/7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7/${INT[0]},${INT[1]},(and on and on 16 times),..../" e1000g.conf
# if $INPUT is bigger than 101, then no changes are needed, so just one if
# statement suffices

This works just fine, but the loooooong sed command is killing me Smilie .
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...
# 2  
Old 05-07-2009
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.
echo '7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7' | nawk -F, -v idx=2 -v val=4 '$(idx+1)=val' OFS=','

# 3  
Old 05-07-2009
Try this:

awk -F"=|," 'BEGIN { printf "Enter the settings delimited by a space: "
getline input < "-"
split(input,line," ")
/ForceSpeedDuplex=/ && line[2]<101 {
{print}' e1000g.conf

# 4  
Old 05-07-2009
Originally Posted by vgersh99
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.
echo '7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7' | nawk -F, -v idx=2 -v val=4 '$(idx+1)=val' OFS=','

I like that. Thanks. I am not that great at awk, and I just couldn't visualize it.

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Positional Parameters Arguments/Variables when using dot (.)

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)
Discussion started by: newbie_01
2 Replies

2. UNIX for Dummies Questions & Answers

Find and replace single character w/awk given conditions

I have a file that looks like this: 14985 DPN verb PPa to spend. 12886 DPNDJN bay tree. 15686 DQ verb to observe 15656 KC ... (7 Replies)
Discussion started by: jvoot
7 Replies

3. UNIX for Dummies Questions & Answers

Print Positional variables

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)
Discussion started by: vanand420
2 Replies

4. Shell Programming and Scripting

Problem with positional variables in BASH

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)
Discussion started by: timmyyyyy
8 Replies

5. Shell Programming and Scripting

awk - setting fs to equal any single character

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)
Discussion started by: chronics
7 Replies

6. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

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)
Discussion started by: dipanchandra
3 Replies

7. Shell Programming and Scripting

How do you print a single quote character in AWK

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)
Discussion started by: cold_Que
6 Replies

8. Shell Programming and Scripting

replace two character strings by two variables with sed command

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)
Discussion started by: POPO10
4 Replies

9. Shell Programming and Scripting

Using two shell variables in single AWK statement

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)
Discussion started by: indianjassi
3 Replies

10. Shell Programming and Scripting

Matching multiples of a single character using sed and awk

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)
Discussion started by: royalibrahim
5 Replies

Featured Tech Videos