Need to replace one string, in a file, with another string from a different file ...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to replace one string, in a file, with another string from a different file ...
# 15  
Old 09-13-2009
Quote:
Originally Posted by know d unknown
the output which got:

Code:
awk -F":" 'NR==FNR{a[$1]=$2; next} NR!=FNR{if(a[$1])print $1," ",a[$1]; else print $0}' shadow2 shadow1
David   $1$FbP0lc8N$MIJ8outnkYyBedIYK7TJ6.
sarvani   $1$0wPwj2dB$GSHkbM9kcPZkGPTdKfZJP1
stuart:$1$sPgpLdir$b9qcdXKeJ78zaZ2tpJuuA/:13924:0:90:7:::

i dono why are you getting the error ??? if you could paste the error that would be greatful
The output is not what is required as you only print fields $1 and $2 if $1 exists in shadow2. Try my snippet above.
# 16  
Old 09-13-2009
@ Praveen:

so is it working fine now ??
# 17  
Old 09-13-2009
Quote:
Originally Posted by know d unknown
i thought you were asking me if the code was wrong...coz it was next to my reply... am sorry i dint get wat u were trying to explain at tat point...
One pitfall of not quoting... but my comment was directed at OP and not at you. It is obvious he mistyped the command.... better to use copy/paste.
# 18  
Old 09-13-2009
thx for pointing out ripat...
the following code will work fine:

awk -F":" 'NR==FNR{a[$1]=$0; next} NR!=FNR{if(a[$1])print a[$1]; else print $0}' shadow2 shadow1
David:$1$FbP0lc8N$MIJ8outnkYyBedIYK7TJ6.:14500:0:90:7:::
sarvani:$1$0wPwj2dB$GSHkbM9kcPZkGPTdKfZJP1:14293:0:90:7:::
stuart:$1$sPgpLdir$b9qcdXKeJ78zaZ2tpJuuA/:13924:0:90:7:::

check if this works fine
# 19  
Old 09-13-2009
Quote:
Originally Posted by Praveen_218
What I need is a file in output called (say shadow3) as below

Code:
David:$1$FbP0lc8N$MIJ8outnkYyBedIYK7TJ6.:14500:0:90:7:::
sarvani:$1$0wPwj2dB$GSHkbM9kcPZkGPTdKfZJP1:14293:0:90:7:::
stuart:$1$sPgpLdir$b9qcdXKeJ78zaZ2tpJuuA/:13924:0:90:7:::

Please note again that the output file contains a password hash from shadow2 file and rest is from shadow1.
In that case the output file you gave is not correct. It should be:

Code:
David:$1$FbP0lc8N$MIJ8outnkYyBedIYK7TJ6.:14166:0:90:7:::
sarvani:$1$0wPwj2dB$GSHkbM9kcPZkGPTdKfZJP1:14293:0:90:7:::
stuart:$1$sPgpLdir$b9qcdXKeJ78zaZ2tpJuuA/:13924:0:90:7:::

# 20  
Old 09-13-2009
Quote:
Originally Posted by ripat
Code:
awk -F: -v OFS=":" 'NR==FNR{_[$1]=$2;next}_[$1]{$2=_[$1]}1' shadow2 shadow1

This worked great for my requirements, Thanks Ripet.

---------- Post updated at 08:26 PM ---------- Previous update was at 08:21 PM ----------

Quote:
Originally Posted by know d unknown
thx for pointing out ripat...
the following code will work fine:

awk -F":" 'NR==FNR{a[$1]=$0; next} NR!=FNR{if(a[$1])print a[$1]; else print $0}' shadow2 shadow1
David:$1$FbP0lc8N$MIJ8outnkYyBedIYK7TJ6.:14500:0:90:7:::
sarvani:$1$0wPwj2dB$GSHkbM9kcPZkGPTdKfZJP1:14293:0:90:7:::
stuart:$1$sPgpLdir$b9qcdXKeJ78zaZ2tpJuuA/:13924:0:90:7:::

check if this works fine
I'm very much thankful to you known_d_unknown for your quick replies; it's outstanding and I really appreciate it. Thanks its working for me now.

Last edited by Praveen_218; 09-13-2009 at 12:05 PM..
# 21  
Old 09-14-2009
For the following code:
Code:
awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1'  shadow2 shadow1 > shadow3

I get syntex error (random one ) as below:
Code:
awk: NR==FNR{a[]=;next}a[]{=a[]}1
awk:           ^ syntax error
awk: fatal: invalid subscript expression

What might be the potential cause?

---------- Post updated 09-15-09 at 12:28 AM ---------- Previous update was 09-14-09 at 11:40 PM ----------

I just noticed that when I create a bash script using the HERE-TAG(from within another bash script) ; in the following manner:

Code:
#!/usr/bin/env bash
# Bash_script001.sh

   echo " Inside Bash_script001.sh";
   ...
   ...
   ...

cat>>"Bash_script002.sh"<<EOF
#!/usr/bin/env bash
# Bash_script002.sh

   echo " Inside Bash_script002.sh";
   ...

   awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1' shadow2 shadow1 > shadow3;
   ...

   ...

EOF

   ....
   echo " Again inside Bash_script001.sh processing rest of Bash_script001.sh ";

Now when the 'Bash_script002.sh' gets created and as it contains the awk script which inturns contains variables $1, $2 as array a[] indexes; the resultant 'Bash_script002.sh' contains only the following:

Code:
awk -F":" -v OFS=":" 'NR==FNR{a[]=;next}a[]{=a[]}1' shadow2 shadow1 > shadow3;

Thats why I'm receiving the syntex error.

Will you please suggest, what can be done to avoid getting the above erronious 'awk' statement in the resultant 'Bash_script002.sh' ?

Thanks in advance and my sincere appreciation for the support so far I received in this forum from all.

Last edited by Praveen_218; 09-14-2009 at 04:11 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

4. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

5. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

How To Replace A String In File With A String Containing Windows File Path

Hi, I have a file with the following contents # Lines that start with a # are comments. # # Calling TOAD like this will perform a comparison from command line : # # "C:\Program Files\Quest Software\Toad for Oracle 9.6\toad.exe" -c... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question