0

I sorted the output from conflicting numbers that are already existing in my DB with new insert request to a file name output. This is the output of the file.

    cat output
    DataBase: (9999999999)  NewDB_insert: (999-999-9999)
    DataBase: (1111111111)  NewDB_insert: (111-111-1111)
    DataBase: (2222222222)  NewDB_insert: (222-222-2222)
    DataBase: (3333333333)  NewDB_insert: (333-333-3333)

I want to run the same command that displays information from the same line that has the conflict. The command just pulls the DB information.

          showinfo -id 9999999999
          ID:9999999999
          Name: Mr.Brown
          City: New York
          Company: Acme
          Client: 1245

         showinfo -id 999-999-9999
         ID:999-999-9999
         Name: Mr.Brown
         City: New York
         Company: Acme
         Client: 1245

I want to use 2 variables to send the output to the screen from my file named output. So I would have the following information from my file.

         **From DB:**
         ID:9999999999
         Name: Mr.Brown
         City: New York
         Company: Acme
         Client: 1245

             **From NewDB_insert:**
             showinfo -id 999-999-9999
             ID:999-999-9999
             Name: Mr.Brown
             City: New York
             Company: Acme
             Client: 1245

So basically I would cat the file in a for loop with one variable as so.

for i  in `cat output | awk '{print $2}' | tr -d ")("`;do echo == From DB:==;showinfo -id $i; echo ==========;done

Which gives me the first part of what I need. I looked online and not able to use any examples to create a for loop with 2 variables. Any help would be appreciated.

1 Answer 1

2

The short answer is probably: "you don't".

You're probably best off using a while loop with read.

sed -e 's/[^(]*(\([^)]*\))[^(]*(\([^)]*\)).*/\1 \2/' output |
while read a b
do
    echo == From DB ==
    showinfo -id $a
    echo =============
    echo == From NewDB_Insert ==
    showinfo -id $b
    echo =======================
done

If you think there might be nasty characters, especially backslashes, in your data, you can use read -r and you can quote "$a" and "$b" in your showinfo lines. If you're sure there won't be any such characters, you're (just about) OK with the code as written.

Note that any variables set in the while loop as written won't be accessible when the loop completes. There are ways around that problem if it is, indeed, a problem.

The sed script looks for:

  • zero or more non-open-parentheses
  • an open parenthesis
  • captures zero or more non-close-parentheses
  • stops capturing
  • a close parenthesis
  • zero of more non-open-parentheses
  • an open parenthesis
  • captures zero of more non-close-parentheses
  • stops capturing
  • a close parenthesis
  • zero or more other characters

and replaces them with the two captured strings (e.g. 9999999999 and 999-999-9999) separated by a space.

Sign up to request clarification or add additional context in comments.

1 Comment

Nicely done. +1. FWIW, another possibly simpler way of extracting the strings is awk -F'[()]' '{print $2, $4}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.