-2

I have File1 having couple of columns and having around 10000 rows like below

VALUE1      VALUE2
ABC11111    9075ABC3
ABC22222    3456ABC4
ABC33333    6786ABC5
ABC44444    9846ABC6
ABC55555    3276ABC7
    

The other file FILE2 have multiple rows (1M+) and having VALUE1 (ABC11111,ABC22222) from FILE1

    We Need to complete ABC11111 Work
    Tomorrow ABC22222 is Holiday
    Hello How are you ABC11111 doing
    His name is ABC55555 Ramesh
    
    

I want To check if VALUE1 is present in FILE2 and if yes need to replace it with VALUE2 (Corresponding of VALUE1), below like output

    We Need to complete ABC11111 Work  --- > We Need to complete 9075ABC3 Work
    Tomorrow ABC22222 is Holiday      ----->  Tomorrow 3456ABC4 is Holiday
    Hello How are you ABC11111 doing   -----> Hello How are you 9075ABC3 doing
    His name is ABC55555 Ramesh   ------->  His name is 3276ABC7 Ramesh

Please Help how this can be achieved in Linux

James Brown
  • 36,089
  • 7
  • 43
  • 59
Ajay
  • 1
  • 1
    SO is not a code writing service; you're expected to show your effort (eg, research, code); consider reviewing [how do I ask a good question](https://stackoverflow.com/help/how-to-ask) and then come back and update the question accordingly; in particular, provide the code you've tried so far and the (wrong) output generated by your code; `sed` or a `bash` looping construct would work but would be extremely slow compared to a solution using `awk`, `perl`, etc. – markp-fuso Feb 22 '22 at 17:52
  • 1
    There must be loads of similar questions with suitable answers. I'd suggest you search a bit more. – glenn jackman Feb 22 '22 at 18:38

1 Answers1

0

how this can be achieved in Linux

I suggest taking look at -f option of sed, it allows you to load sedscript file into sed, if you need to do replacement as follows

ABC11111    9075ABC3
ABC22222    3456ABC4
ABC33333    6786ABC5
ABC44444    9846ABC6
ABC55555    3276ABC7

then sedscript might look as follows

s/ABC11111/9075ABC3/g
s/ABC22222/3456ABC4/g
s/ABC33333/6786ABC5/g
s/ABC44444/9846ABC6/g
s/ABC55555/3276ABC7/g

and lets name it replaces.sed and your file file.txt then

sed -f replaces.sed file.txt

will output to standard output file with described changes applied. As your replacement has around 10000 rows you should find to way automatically convert such file into sedscript, which is not hard to do in GNU AWK. Be careful if replacements might contain character of special meaning for sed e.g. ., this is not problem if all replacements consist solely of ASCII letters and digits as is case for snippet you shown.

Daweo
  • 31,313
  • 3
  • 12
  • 25