Read 2 file line by line @ same time

HI All,

I am aware of reading one file line by line but now my requirement is to read 2 file line by line as below explained.
I have 2 files as below file 1 with spaces and file 2 same as file1 but without space and () brackets
now i have to read both file line by line and use first line of each file in some command

Desired output : abcdef ghi abcd ab ######################  abcdefghiabcdab
                                abcd 12345                 ######################  abcd12345
                                abdfd sdff ert (sdfre ere ert rrt)  ######################  abdfdsdffertsdfreereertrrt

File 1

abcdef ghi abcd ab
abcd12345
abdfd sdff ert (sdfre ere ert rrt)

File 2

abcdefghiabcdab
abcd12345
abdfdsdffertsdfreereertrrt

Please advise.

For a simple concatenation of the lines, consider paste

paste file1 file2

More flexible is the shell

while read L1 <&3 && read L2 <&4
do
  echo "$L1 #### $L2"
done 3< file1 4< file2
5 Likes

Need help , i have 2 files

file 1
abc def ijk ca 
def fff ere we
file 2
abcdefijkca
defffferewe
wrote below 
#!/bin/bash
paste file11 file22 | (
while read col1 col2 ; do
echo "\"${col1}\" ${col2}"
done
)
Output getting : "abc" def ijk ca       abcdefijkca
                                 "def" fff ere we      defffferewe

Desired Output : "abc def ijk ca"       abcdefijkca
                                 "def fff ere we"       defffferewe

Please advise.

IFS=$'\t'

--- Post updated at 12:30 ---

paste <(xargs -d'\n' -n1 <file1 -I{} echo \"{}\") file2

Couldn't get it IFS=$'\t'
Please be kind to suggest where we need to put it.

--- Post updated at 09:39 AM ---

Please advise
Where to use this IFS=$'\t'

paste <(xargs -d'\n' -n1 <file1 -I{} echo \"{}\") file2
getting xargs: illegal option -- d
awk '{printf "\42"$0"\42\t"; getline < "file2"} 1' file1

--- Post updated at 12:47 ---

#!/bin/bash
IFS=$'\t'

Tried both...none of them are working to produce Desired Output as below

"abc def ijk ca" abcdefijkca
"def fff ere we" defffferewe

A great example was in post #2

#!/bin/bash
while read col1 <&3 && read col2 <&4
do
        echo "\"$col1\" $col2"
done 3< file1 4< file2

Try also

paste -d'"" '  /dev/null file1 /dev/null file2 
"abc def ijk ca" abcdefijkca
"def fff ere we" defffferewe

EDIT: or this slight modification of nezabudka's proposal:

awk '{getline T < "file2"; print DQ $0 DQ, T}' DQ='"' file1
"abc def ijk ca" abcdefijkca
 "def fff ere we" defffferewe

Or your own approach with the correct IFS value:

paste file1 file2 | while IFS=$'\t' read col1 col2 ; do echo "\"${col1}\" ${col2}"; done
"abc def ijk ca" abcdefijkca
"def fff ere we" defffferewe
1 Like

Slight modification in MadeInGermany's solution

$ while read L1 <&3 && read L2 <&4
> do
> echo \"$L1\" $L2
> done 3< file1 4< file2
"abc def ijk ca" abcdefijkca
"def fff ere we" defffferewe
$

Thanks Rudic for your help,now i am getting the desired output but what i am trying to achieve is below
i have to pass col1 and col2 values to keytool command to have it like below

keytool -export -alias "abc def ijk ca" -file abcdefijkca.der -keystore ts.jks -storepass xxxx
keytool -export -alias "def fff ere we" -file defffferewe.der -keystore ts.jks -storepass xxxx

Tried implement as below

#!/bin/bash
#set -x
paste file1 file2 | while IFS=$'\t' read col1 col2 ;
do echo "\"${col1}\" ${col2}";
keytool -export -alias "\"${col1}\" -file ${col2}".der -keystore ts.jks -storepass xxxx;
done

but after executing it i am getting below error as < is getting appended to col1 and col2 as below

"abc def ijk ca" abcdefijkca
Java HotSpot(TM) 64-Bit Server VM warning: PICL (libpicl.so.1) is missing. Performance will not be optimal.
keytool error: java.lang.Exception: Alias <"abc def ijk ca" -file abcdefijkca.der> does not exist
"def fff ere we" defffferewe
Java HotSpot(TM) 64-Bit Server VM warning: PICL (libpicl.so.1) is missing. Performance will not be optimal.
keytool error: java.lang.Exception: Alias <"def fff ere we" -file defffferewe.der> does not exist

Please advise

Check your quoting - you're quoting both col variables plus the -file constant into one argument. Quote them separately.

Omit the explicit \" quotes. I think you had the quotes on the command line, to prevent the shell from word-splitting (each word becomes an argument).
And, since you are going to use a while loop, prefer the builtin read command.

while read col1 <&3 && read col2 <&4; do
    keytool -export -alias "$col1" -file "$col2".der -keystore ts.jks -storepass xxxx
done 3< file1 4< file2

Thanks , this worked like a charm :b: