Replacing from a list file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing from a list file using awk
# 1  
Old 12-16-2008
Replacing from a list file using awk

I have a set of user name password pairs in one file and I have a set of files which have user names and a random string as the password. How can I read the user name and password from the first file and replace the random password in the second file.

my list file looks like this

user1, pass1
user2, pass2
user3, pass3

the other files (the ones I want to replace) look like this.

line 1 bla bla bla
line 2 userx
line 3 skip line
line 4 skip line
line 5 randompass
more lines...

so far I have this awk command that does replaces one password at a time

gawk /"user1"/{l=NR}l&&NR==l+3{sub(/"randompass"/,"\"pass1\"")}1 file1.txt > newfile1.txt

The problem is that not all the files contain user1 so I need to grab the first row of my list file then replace in all the files that have user1 then read the second row replace and so on.
All ideas are welcome. Thanks.
# 2  
Old 12-16-2008
Code:
open FH,"<b.sh";
while(<FH>){
	@arr=split(",",$_);
	$hash{$arr[0]}=$arr[1];
}
close FH;
open FH,"<a.pl";
while(<FH>){
	$pass=$hash{$1} if (m/(user.)/);
	if(m/pass/){
		$_=~s/randompass/$pass/;
		print $_;
	}
	else{
		print $_;
	}
}

# 3  
Old 12-16-2008
replace pawword

Quote:
Originally Posted by summer_cherry
Code:
open FH,"<b.sh";
while(<FH>){
    @arr=split(",",$_);
    $hash{$arr[0]}=$arr[1];
}
close FH;
open FH,"<a.pl";
while(<FH>){
    $pass=$hash{$1} if (m/(user.)/);
    if(m/pass/){
        $_=~s/randompass/$pass/;
        print $_;
    }
    else{
        print $_;
    }
}

Quick question. Does this assume that I know the old password (random password)? If that is the case the code will not work in my case. The only thing I know about the previous password is that it is two lines after theuser name so that is why my awk code above adds 3 to the NR. So I'm looking for a way to pass the read user name into my awk.
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing last line with awk and change the file name

Hi Guys, I am having a set of date format files files where I am performing the below set of operations in the files . I Need to replace the last line value with specific date which is a pipe delimited file. for egf1_20140101.txt aa|aus|20140101|yy bb|nz|20140101|yy . .... (19 Replies)
Discussion started by: rohit_shinez
19 Replies

2. Shell Programming and Scripting

awk Array matching and replacing from master file.

I have an awk related question that I was hoping you all could help with. I am given 2 input files named OLDFILE and NEWFILE, and a Master file named MASTERFILE. They can be seen below. OLDFILE: a a a a a f g 4 5 7 8 1 2 3 (1 Reply)
Discussion started by: tiktak292
1 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. HP-UX

Replacing Hex Characters In A File Using awk?

Hi guys, First off, i'm a complete noob to UNIX and LINUX so apologies if I don't understand the basics! I have a file which contains a hex value of '0D' at the end of each line when I look at it in a hex viewer. I need to change it so it contains a hex value of '0D0A0A' I thought... (10 Replies)
Discussion started by: AndyBSG
10 Replies

5. Shell Programming and Scripting

Replacing FQDN by hostnames in a CSV file with sed & awk

Hello, Beginning with shell scipting, I'm trying to find in a csv file, the lines where the field related to hostname is displayed as an FQDN intead the hostname. (some lines are correct) and the to correct that inside the file: Novell,11.0,UNIX Server,bscpsiws02,TxffnX1tX1HiDoyBerrzWA==... (2 Replies)
Discussion started by: Wonto
2 Replies

6. Shell Programming and Scripting

Help with awk replacing identical columns based on another file

Hello, I am using Awk in UBUNTU 12.04. I have a file like following with three fields and 44706 rows. F1 A A F2 G G F3 A T I have another file like this: AL_1 F1 A A AL_2 F1 A T AL_3 F1 A A AL_1 F2 G G AL_2 F2 G A AL_3 F2 G G BO_1 F1 A A BO_2 F1 A T... (6 Replies)
Discussion started by: Homa
6 Replies

7. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

8. Shell Programming and Scripting

Awk replacing file with user input

this section of the awk code i have here takes file to work with from the user. the user specifies the file name from the command line and the file name is assigned to the variable $FLIST awk 'BEGIN { while((getline < "'${FLIST}'")>0) S FS="\n"; RS="}\n" } now, i dont want... (5 Replies)
Discussion started by: SkySmart
5 Replies

9. UNIX for Dummies Questions & Answers

Script for replacing text in a file based on list

Hi All, I am fairly new to the world of Unix, and I am looking for a way to replace a line of text in a file with a delimited array of values. I have an aliases file that is currently in use on our mail server that we are migrating off of. Until the migration is complete, the server must stay... (8 Replies)
Discussion started by: phoenixjc
8 Replies

10. Shell Programming and Scripting

Replacing or removing a long list of pattern by using awk or sed

Input: >abc|123456|def|EXIT| >abc|203456|def|EXIT2| >abc|234056|def|EXIT3| >abc|340056|def|EXIT4| >abc|456000|def|EXIT5| . . . Output: def|EXIT| def|EXIT2| def|EXIT3| def|EXIT4| def|EXIT5| . . My try code: (9 Replies)
Discussion started by: patrick87
9 Replies
Login or Register to Ask a Question