0

I have below two variables: $var1 which has value like

ABC
XYZ
SDF

and $var 2 which has value like

SDF
SGDGH
hfg

I want the output like

ABC
XYZ
SDF
SDF
SGDGH
hfg

I have the below script and i want to merger the value of file_import_2 and file_import_3 into one.

    file_import_2=""
    file_import_3=""
    for DataBase in $(< "$1"); do

    file_import_1=`grep -iw "CRQ_DI_PROD_$DataBase" "$Import_List_File" | grep -i prod`;
    if [[ "$file_import_1" = GDH_* ]]; then
    file_import_2=$file_import_1

    fi
    if [[ "$file_import_1" != GDH_* ]]; then
    file_import_3=$DataBase
    fi

var3=$(printf '%s\n%s\n' "$file_import_2" "$file_import_3")
echo $var3

    done

The file i am passing to the script and Import_List_File have 4 values each

FARP_DATA_111
TRIN_STAGING
DBH
PRS

With the print f solution, output is:

CRQ_DI_PROD_FARP_DATA_111
CRQ_DI_PROD_TRIN_STAGING
CRQ_DI_PROD_TRIN_STAGING DBH
CRQ_DI_PROD_TRIN_STAGING PRS

1 Answers1

1

Here you go:

$ printf '%s\n' "$var1" "$var2"
ABC
XYZ
SDF
SDF
SGDGH
hfg

To assign that to another variable: var3=$(printf '%s\n%s\n' "$var1" "$var2").

markgraf
  • 2,860
  • Thanks a lot for that. I added the code you sent to my sample script and i am getting a little different value. Can you please help me with that? – user3901666 Aug 30 '19 at 10:13
  • 1
    btw, this doesn't need two %s\ns in the printf format string. printf will loop over args if there are more than are needed by the fmt. – cas Aug 30 '19 at 10:37
  • Thanks cas, I didn't think of that. – markgraf Aug 30 '19 at 13:13
  • I wasn't sure if this was bash or GNU coreutils only but it's "standard". in the posix printf spec it says "9. The format operand shall be reused as often as necessary to satisfy the argument operands. ......." – cas Sep 01 '19 at 03:05