Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK search and replace

Status
Not open for further replies.

darkmuse

ISP
Aug 2, 2002
12
US
Hello all,
I'm new to this site and have what I hope is a simple AWK problem...

I need to search a file and add text to a line if the criteria is correct.

If I have the following file:

bob Password = "password"
Service-Type=Framed-User
Session-Timeout=9000
Ascend-Idle-Limit=900
steve Password = "password"
Service-Type=Framed-User
Session-Timeout=9000
Ascend-Idle-Limit=900

I need to search for an EXACT MATCH for bob at the BEGINNING of the line and include some text to the same line bob is on.

To make the file look like this when finished:

bob Password = "password-disconnected 08-01-02"
Service-Type=Framed-User
Session-Timeout=9000
Ascend-Idle-Limit=900
steve Password = "password"
Service-Type=Framed-User
Session-Timeout=9000
Ascend-Idle-Limit=900


Any help would be appreciated.
Thanks in advance.
 
Darkmuse:

field $4 is the one you want to change if field $1 starts with bob:

Regards,

Ed


awk ' { if($1 ~ /^bob/)
$4="\"password discontconnected 08-01-02\""
print $0
} ' e.file
 
Darkmuse:

I forgot the $ at the end of the regular expression:

nawk ' { if($1 ~ /^bob$/)
$4="\"password disconnected 08-01-02\""
print $0
} ' e.file > new.file

you need that for an exact match.

BTW, welcome to the forum.

Regards,

Ed
 
Thanks Ed. Thats a start, and that answers my question. The problem is, I didn't word my question correctly. :)

Let me try again...

I need to open a file i'll call "radius" and do a line by line search the field $1 for a variable i'll call "bob" and append some text to the field $4 variable on the same line as "bob".



If I have the following file:

bob Password = "hispassword"
Service-Type = "Framed-User"
Session-Timeout = "9000"
Ascend-Idle-Limit = "900"
steve Password = "mypassword"
Service-Type = "Framed-User"
Session-Timeout = "9000"
Ascend-Idle-Limit = "900"

I need to search for an EXACT MATCH for bob at the BEGINNING of the line and include some text to the same line bob is on. I need to append to field $4 because the password field will be different with each person.

To make the file look like this when finished:

bob Password = "hispassword-disconnected 08-01-02"
Service-Type = "Framed-User"
Session-Timeout = "9000"
Ascend-Idle-Limit = "900"
steve Password = "mypassword"
Service-Type = "Framed-User"
Session-Timeout = "9000"
Ascend-Idle-Limit = "900"


I hope this clears it up a little.
 
Code:
awk -v name=bob -v date=`date +%m-%d-%y` '
{ if ($1 ~ "^"name"$")
    $4 = substr($4, 1, length($4)-1) " discontinued " date "\"";
}
{ print; }
' file > file.new
[code]
 
Thanks again to everyone.

I have one more question and hopefully i'll be finished.

Taking the previous posting:

awk -v name=bob -v date=`date +%m-%d-%y` '
{ if ($1 ~ "^"name"$")
$4 = substr($4, 1, length($4)-1) " discontinued " date "\"";
}
{ print; }
' file > file.new

After speaking to some people here at work, there are a couple more things that would make this script perfect for what were trying to do.

Each month there will be a list of 400-600 usernames that I will have in a line delimited text file (file.users). How can the above script be modified to pull those usernames from file.users and insert them in the name$?

Also if for some reason AWK searches for the name$ and it isn't found in the original file, I would like to print that name$ to a separate file called file.users.notfound
I'm assuming this can be done in the if statement, but i'm not sure how exactly to accomplish it.


Thanks again,
Randall
 
This may do what you want.
Code:
BEGIN {
   num = 0
   while ((getline < &quot;file.users&quot;) > 0) {
      nam[num] = $0
      num++
   }
}
{
  for (j=0;j<num;j++) {
    if ($1 ~ &quot;^&quot;nam[j]&quot;$&quot;) {
      $4 = substr($4, 1, length($4)-1) &quot; discontinued &quot; date &quot;\&quot;&quot;
      flg[j] = 1
    }
  }
  print
}
END {
     for (j=0;j<num;j++) if (!flg[j]) print nam[j] > &quot;file.users.notfound&quot;
}
Run it without the -v name=bob. CaKiwi
 
Thanks CaKiwi.

The script seems to work great except for one thing. It doesn't preserve the format of the original user file.

I have saved the script in a file called automate.

automate:

BEGIN {
num = 0
while ((getline < &quot;file.users&quot;) > 0) {
nam[num] = $0
num++
}
}
{
for (j=0;j<num;j++) {
if ($1 ~ &quot;^&quot;nam[j]&quot;$&quot;) {
$4 = substr($4, 1, length($4)-1) &quot;-discontinued &quot; date&quot;\&quot;&quot;
flg[j] = 1
}
}
print
}
END {
for (j=0;j<num;j++) if (!flg[j]) print nam[j] > &quot;file.users.notfound&quot;
}


This is the original users file.

users:

bob<TAB> Password = &quot;a1&quot;
<TAB> Service-Type = &quot;Framed-User&quot;,
<TAB> Session-Timeout = &quot;9000&quot;,
<TAB> Ascend-Idle-Limit = &quot;900&quot;
bob2<TAB> Password = &quot;ab12&quot;
<TAB> Service-Type = &quot;Framed-User&quot;,
<TAB> Session-Timeout = &quot;9000&quot;,
<TAB> Ascend-Idle-Limit = &quot;900&quot;
tom<TAB> Password = &quot;abc123&quot;
<TAB> Service-Type = &quot;Framed-User&quot;,
<TAB> Session-Timeout = &quot;9000&quot;,
<TAB> Ascend-Idle-Limit = &quot;900&quot;
steve<TAB>Password = &quot;abcd1234&quot;
<TAB> Service-Type = &quot;Framed-User&quot;,
<TAB> Session-Timeout = &quot;9000&quot;,
<TAB> Ascend-Idle-Limit = &quot;900&quot;


This is the command I execute:
awk -f automate date=`date +%m-%d-%y` users > users.new

This is the result.

users.new

bob Password = &quot;a1-discontinued 08-08-02&quot;
Service-Type = &quot;Framed-User&quot;,
Session-Timeout = &quot;9000&quot;,
Ascend-Idle-Limit = &quot;900&quot;
bob2 Password = &quot;ab12&quot;
Service-Type = &quot;Framed-User&quot;,
Session-Timeout = &quot;9000&quot;,
Ascend-Idle-Limit = &quot;900&quot;
tom Password = &quot;abc123&quot;
Service-Type = &quot;Framed-User&quot;,
Session-Timeout = &quot;9000&quot;,
Ascend-Idle-Limit = &quot;900&quot;
steve Password = &quot;abcd1234&quot;
Service-Type = &quot;Framed-User&quot;,
Session-Timeout = &quot;9000&quot;,
Ascend-Idle-Limit = &quot;900&quot;




All the tabs are replaced with spaces for some reason.


Is it possible to correct this?







 
Try this.

BEGIN {
num = 0
while ((getline < &quot;file.users&quot;) > 0) {
nam[num] = $0
num++
}
}
{
for (j=0;j<num;j++) {
if ($1 ~ &quot;^&quot;nam[j]&quot;$&quot;) {
$0 = substr($0, 1, length($0)-1) &quot; discontinued &quot; date &quot;\&quot;&quot;;
flg[j] = 1
}
}
print
}
END {
for (j=0;j<num;j++) if (!flg[j]) print nam[j] > &quot;file.users.notfound&quot;
}
CaKiwi
 
That was it!!!!

From all the people who've had to do this manually..

Thanks to everyone, very much.

Randall
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top