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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing a field on a particular line. Pls HELP!

Status
Not open for further replies.

networkguy

Technical User
Jun 12, 2001
3
US
I am trying to replace a particular field in a particular line. There are 2 choices: 1) line begins with name "eric"; 2) line number 2.
This is a script that replaces a field. How can I point it to the specific line???

#! /bin/ksh
read username?"Please enter your username:"
read password?"Please enter your password:"
nawk -v user=$username -v passwd=$password 'BEGIN {FS=";";OFS=";"} { $12=passwd;print }' input > output

Eric
 
This should work as is, but may require a little tweaking.
Code:
#! /bin/ksh
read username?"Please enter your username:"
read password?"Please enter your password:"
nawk -v user=$username -v passwd=$password 'BEGIN {FS=";";OFS=";"} NR==2 {$12=passwd;print} $1=="eric" {$12=passwd;print}' input > output
Greg.
 
Or to shorten it slightly, you could do:
Code:
#! /bin/ksh
read username?"Please enter your username:"
read password?"Please enter your password:"
nawk -v user=$username -v passwd=$password 'BEGIN {FS=";";OFS=";"} NR==2 || $1=="eric" {$12=passwd;print}' input > output
Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top