How to assign correct values to the multiple words?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to assign correct values to the multiple words?
# 1  
Old 10-24-2016
How to assign correct values to the multiple words?

The file1 contains mistakes and looks like

Code:
1 No one have never become rich by giving.
Anne Dickens
2 No one is worthless in this globe who lightens the weights of other.
Charles_Dickens

file2 contains the correction of words and looks like

Code:
rich    poor
have    has
never    ever
Dickens    Frank
worthless    useless
globe    world
weights    burdens 
other    another

the desired output file3 should be like

Code:
1 No one have never become rich by giving.    @    No one has ever become poor by giving.
Anne Dickens    @    Anne Frank
2 No one is worthless in this globe who lightens the weights of other.    @    No one is useless in this world who lightens the burdens of another.
Charles_Dickens    @    Charles_Dickens

This is what I did

Code:
awk 'NR==FNR {a[$1]=$2;next} {for ( i in a) gsub(i,a[i])}1' file2 file1 > file3
cat file3

1 No one has ever become poor by giving.
Anne Frank
2 No one is useless in this world who lightens the burdens of another.
Charles_Frank

The first problem is its is not strictly identifying the words as Charles_Dickens should be different than Dickens. Then it is very slow for big files and at the end I am not able generate like desired output.
Any other bash/perl solution or the correction in the code will be very helpful.


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 10-24-2016 at 12:48 PM.. Reason: Changed CODE tags.
# 2  
Old 10-24-2016
This seems to cure the failures you quote except for the processing speed - I'm afraid you can't do faster...
Code:
awk '
NR == FNR       {a[$1] = $2
                 next
                }
                {printf "%s\t@\t", $0
                 L = sub (/\.$/, _)
                 for (i=1; i<=NF; i++) if ($i in a) $i = a[$i]
                 if ($1+0) sub ($1 FS, _)
                 printf "%s%s\n", $0, L?".":""
                }
' file2 file1
1 No one have never become rich by giving.	@	No one has ever become poor by giving.
Anne Dickens	@	Anne Frank
2 No one is worthless in this globe who lightens the weights of other.	@	No one is useless in this world who lightens the burdens of another.
Charles_Dickens	@	Charles_Dickens

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-25-2016
Hi RudiC,
Kindly explain below part of code.
Code:
if ($1+0) sub ($1 FS, _)

I could understand that first we are checking if first field is number then substituting it and space with _. If it is so then why.?

Last edited by looney; 10-25-2016 at 12:45 AM..
# 4  
Old 10-25-2016
Quote:
Originally Posted by looney
Hi RudiC,
Kindly explain below part of code.
Code:
if ($1+0) sub ($1 FS, _)

I could understand that first we are checking if first field is number then substituting it and space with _. If it is so then why.?
Hello looney,

Could you please go thorugh following and let me know if this helps you.
Code:
if ($1+0)   sub ($1 FS, _)    #### In if condition it is mentioned $1+0, so by adding 0 to $1's value we are making sure it should take only 
                              #### integer values in first field and then subsitutue this first field value with space to 
                              #### NULL(you could take _ as a variable whose value is NULL). So first we are printing the value of $0 with printf 
                              #### the actual one, then we are editig the line as per OP's requirement so if you see OP doesn't want $1(first field)
                              #### in 2nd part of line where we are printing the exchanged values so this is why it is used here.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to sort two fields and printf not displaying the correct values

Not able to sorting two fileds resolved printf issue 01-1000/9|JAN 01-0000/6|MAN 01-1010/2|JAN 01-1010/2|JAN 01-1010/2|JAN 01-1000/9|JAN 01-1000/9|JAN 01-1000/9|SAA 01-1000/9|SAA 01-0000/6|SAN 01-0000/6|SAN 1.sort -t'|' -k1,1n -k2,2 file (3 Replies)
Discussion started by: kalia4u
3 Replies

2. AIX

Restvg does not assign the correct PP size to volume group

hello, i am running an AIX6.1 machine and i am trying to restore a volume group that i backed up using mkvgdata command from another server. although i checked file .data and i make sure that PP size for this volume group is 128, when i run restvg command to restore it, it fails because it... (2 Replies)
Discussion started by: omonoiatis9
2 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

Problem while assign string (words with tab) to a variable

Hi, I have a String(words with tab space) in a file ->file1.txt 0xxxx 11 test $aa$ 8.43 when i read the file and assign to variable value=$(cat file1.txt) echo $value i get the output without tab spaces. 0xxxx 11 test $aa$ 8.43 How to assign string... (2 Replies)
Discussion started by: nanthagopal
2 Replies

5. Shell Programming and Scripting

Need to parse lines in a file into two words and assign the values to two variables

For example, I have a file with below lines containing VOB tags and VOB paths. * /vobs/fts/FTSUSM20_VOB /ccvobsslx01/projects/vobs/eml/FTSUSM20_VOB * /vobs/fts/FTS20_VOB /ccvobsslx01/projects/vobs/eml/FTS20_VOB * /vobs/pmv/PMS_VOB /ccvobsslx01/projects/vobs/cpm/_/PMS_VOB *... (4 Replies)
Discussion started by: senthilkc
4 Replies

6. Shell Programming and Scripting

Assign values to variable

Hi Masters, I want to assign the values of one variable to another variable. Here the varaible name 'var' is dynamic. I know the values of V_2 and U_3, but If the i/p of TYPE is 'U' and the NO is 3, then I want to assign the values of U_3 to var. How we can achieve it? TYPE="U"... (4 Replies)
Discussion started by: ecearund
4 Replies

7. Shell Programming and Scripting

Assign words in a string to array

I have a string as "yes why not" I want to create one array variable with contents as one word per place in array.. for above string,the array variable should contain... x="yes,why,not" x = yes x = why x = not Please help me,I am stuck up in the problem since 2 days... (3 Replies)
Discussion started by: uday26
3 Replies

8. Shell Programming and Scripting

Select multiple values from an Oracle database and assign it to two dimensional array

hi I have two tables in oracle DB and am using a joining query which will result in the output as follows. i need to assign it to a two dimensional array and use it for my further calculations. the way i tried is as follows. #!/bin/ksh export... (1 Reply)
Discussion started by: aemunathan
1 Replies

9. Shell Programming and Scripting

how to assign multiple values in a pl/sql script

Hello friends, This query is with regards to a script (pl/sql) which returns multiple values. Please see below script wherein the query returns a single value and is assigned to a single variable DB_VALID_CDR=`sqlplus -s user/pass<<!EOF | grep -v "^Connected" 2>&1 set termout off echo... (2 Replies)
Discussion started by: vivek_damodaran
2 Replies

10. UNIX for Dummies Questions & Answers

how to assign correct control flow?

how can i create a script with a correct control flow/loop(?) to provide output from 3 files? file1 one two three file2 yellow red orange file3 banana apples cantaloupes output file: one (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question