0

I want to replace some text of another file if match is found and position wise.

First file:

abc ram
bdc jay 
vlm rock

Second file:

grep -f -w '' /d/demo.txt  
grep -f -w '' /d/demo.txt 
grep -f -w '' /d/demo.txt 

As you can see the first file count is 3 and second file count is 3. My output should be like:

grep -f -w 'abc ram' /d/demo.txt  
grep -f -w 'bdc jay' /d/demo.txt 
grep -f -w 'vlm rock' /d/demo.txt 

Position of first file value should be put in second file first position and so on.

abc ram

grep -f -w 'abc ram' /d/demo.txt 

I have written some code but not able to build logic .. up to match two file count is done inside logic not able to do.

if [ $count_firstfile == $count_secondfile ]
then 
      // Logic needed here 
      
elif [ $count_firstfile != $count_secondfile ]    
then 

else 
    echo "Nothing to do"
fi  
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Note that `[ $anything == $anything_else ]` is buggy for multiple reasons. Use `[ "$anything" = "$anything_else" ]`, with each variable quoted, and only a single `=` rather than `==`. (Some shells, including bash, include an implementation of `test` that permits `==` to be used, but making code work only on bash and not baseline POSIX shells like dash or ash is not good practice; and leaving out the quotes introduces a wide variety of bugs depending on what values are in use at runtime). – Charles Duffy Jun 29 '20 at 16:02
  • 1
    It looks like you're [writing a script to generate another script](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). How about we help you cut out the middle man and have the first script run the `grep` calls directly? – John Kugelman Jun 29 '20 at 16:03

2 Answers2

1

The general practice you need is given in Read from two files line by line and process them simultaneously.

#!/usr/bin/env bash
case $BASH_VERSION in '') echo "This script requires bash" >&2; exit 1;; esac

sigil="''"
while IFS= read -r command_line <&3 && IFS= read -r arg_line <&4; do
  [[ $command_line = *"$sigil"* ]] || { echo "ERROR: No sigil in $command_line" >&2; exit 1; }
  printf -v arg_line_q '%q' "$arg_line"
  printf '%s\n' "${command_line//$sigil/$arg_line_q}"
done 3<file_with_commands 4<file_with_arguments >combined_output_file

This reads from file_with_commands and file_with_arguments, and writes to combined_output_file. Note that in bash 5, you could use ${arg_line@Q} to avoid needing the printf -v arg_line_q %q "$arg_line" line.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • … can you explain the above code in detail not able to understand what is happening.... how do i pass file to the above script – rarkatatruenayu Jun 30 '20 at 05:20
  • … … can you explain the above code in detail not able to understand what is happening.... how do i pass file to the above script – rarkatatruenayu Jun 30 '20 at 05:20
0

Code logic aside, the following command should produce the expected result:

grep -Ff First_file /d/demo.txt
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
LeadingEdger
  • 604
  • 4
  • 7