0

I need to some strings in a file in the following fashion

Old file:

Real/Test1
Real/Test1
Real/Test2
Real/Test3
Real/Test3
Real/Test4

New File:

Real/Test1  a1 b1 c1 d1
Real/Test1  a1 b1 c1 d1
Real/Test2  a2 b2 c2 d2
Real/Test3  a3 b3 c3 d3
Real/Test3  a3 b3 c3 d3
Real/Test4  a4 b4 c4 d4

I have an intermediate file which has the old string in the column 1 and then the new string, something like this.

Test1 a1 b1 c1 d1
Test2 a2 b2 c2 d2
Test3 a3 b3 c3 d3
Test4 a4 b4 c4 d4

Could anyone please help with this?

With my very primitive knowledge, I tried following:

(while read n1 n2 do set n1 n2 sed -i "s/$n1/$n1 $n2/g" old > final done)

where "old" and "intermediate" inputs are the content mentioned above.

Thanks a lot !

  • 4
    SE is not a free coding service. I'm certain you tried something already, add that to your question. – grochmal May 9 '17 at 21:30
  • Agree, let's see what you've tried already, see if we can collectively get you on the right track if it's not working quite right. – steve May 9 '17 at 21:50
  • Thanks Grochmal and Steve for asking for the trial I am making. My scripting ability is very primitive and therefore I was hesitant putting my attempts here..So what I did is following: (while read n1 n2 do set n1 n2 sed -i "s/$n1/$n1 $n2/g" old > final done)<intermediate where old is the old file as I wrote in my question and intermediate is the intermediate file. – ashu May 9 '17 at 21:57
0
perl -lne '
   @ARGV and $h{$1}=s/(\S+)//r,next;
   s|/(\S+)\K|$h{$1}|;print;
' intermediate.file old.file

Results

Real/Test1 a1 b1 c1 d1
Real/Test1 a1 b1 c1 d1
Real/Test2 a2 b2 c2 d2
Real/Test3 a3 b3 c3 d3
Real/Test3 a3 b3 c3 d3
Real/Test4 a4 b4 c4 d4

Explanation

  • Using the intermediate file (@ARGV is > 0) we populate the hash using the first field as the key and the remaining fields as the corresponding value.
  • When we process the old file (@ARGV = 0), we look at the string after the slash and use that to pullup the hash value and put it back in the current line.
2

Since your files appear to be sorted in order of the join field, you could use the join command fairly easily e.g.

join old <(sed 's;^;Real/;' intermediate)

or (if your shell doesn't support process substitution)

sed 's;^;Real/;' intermediate | join old -

ex.

$ sed 's;^;Real/;' intermediate | join old -
Real/Test1 a1 b1 c1 d1
Real/Test1 a1 b1 c1 d1
Real/Test2 a2 b2 c2 d2
Real/Test3 a3 b3 c3 d3
Real/Test3 a3 b3 c3 d3
Real/Test4 a4 b4 c4 d4
1

Try something like this with your gnu awk :

awk -F"[/ ]" 'NR==FNR {a[$1]=$2OFS$3OFS$4;next}$2 in a {print $0,a[$2]}'  intermediatefile oldfile >newfile
  • Thanks George, but this doesn't take care of repeated lines in the old file. – ashu May 9 '17 at 22:38
  • @ashu Yeah, right. Try the revised code. Mind that file read order has been reversed now. – George Vasiliou May 9 '17 at 22:48
  • Sorry, this is printing the old file. – ashu May 9 '17 at 22:52
  • @ashu I had a typo. Try again please. Sorry i am on the road and i can not test my proposal... – George Vasiliou May 9 '17 at 22:57
  • Thanks so much George, still one problem, spaces between columns 2-4 are lost, they come together. – ashu May 9 '17 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.