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
user3901666
  • 243
  • 4
  • 11
  • It's a variable – user3901666 Aug 30 '19 at 09:40
  • Have you tried `printf "%s\n" "$var1" "$var2"` ? – cas Aug 30 '19 at 10:23
  • yes getting the same output mentioned in the edit . – user3901666 Aug 30 '19 at 10:34
  • First two are fine then i m getting like this in one line CRQ_DI_PROD_TRIN_STAGING DBH CRQ_DI_PROD_TRIN_STAGING PRS instead of just DBH and PRS – user3901666 Aug 30 '19 at 10:35
  • **always** double-quote your variables to prevent shell from word-splitting etc them (e.g. to retain the LF characters). e.g. `file_import_2="$file_import_1"` and `file_import_3="$DataBase"`. Ditto for command substitution, e.g. `var3="$(printf '%s\n' "$file_import_2" "$file_import_3")"`. See [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters) – cas Aug 30 '19 at 10:42
  • Provided the double quotes as suggested , but still getting the same result. – user3901666 Aug 30 '19 at 10:48
  • Is there anything wrong with below? file_import_2="" file_import_3="" – user3901666 Aug 30 '19 at 10:49
  • what? you want to set them to an empty string? – cas Aug 30 '19 at 11:07
  • Yes..I mean i have initialized them to null in the beginning – user3901666 Aug 30 '19 at 11:29
  • that's ok. you need to double-quote variables when you *use* them. e.g. `foo="$bar"`. If there's any chance that the right-hand-side of an assignment might contain whitespace or shell meta-characters (like `&`) then you need to quote the RHS, with either single-quotes (for literal, fixed, static text) or double-quotes (if you want variable expansion, command substitution, etc to happen). – cas Aug 30 '19 at 11:38

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,849
  • 1
  • 8
  • 20
  • 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\n`s 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](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html) 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