Skip to main content
added 134 characters in body
Source Link
ilkkachu
  • 148.2k
  • 16
  • 268
  • 441

In Bash (or some other shell that supports $'' and arrays), this should do, as long your input files don't contain tabs(*):

IFS=$'\t' paste arg1.txt arg2.txt | while read -r -a A ; do [ "${#A[@]}" -eq 2 ] && printf "%s - %s\n" ${A[@]} done 

paste sticks the input files together line-by-line, read -a A reads the columns to the array A, using tab (from IFS) as a separator. [ "${#A[@]}" -eq 2 ] checks that the array has exactly two members, and ${A[@]} drops them on the command line of printf. Change the command as required.

(* if you need to support tabs, I'd move to using e.g. Perl)

With these input files:

$ cat arg1.txt foo bar doo $ cat arg2.txt one two two three three three 

The output from the above snippet is:

foo bar - one doo - two two 

The last line from arg2.txt is ignored, since arg1.txt doesn't have a corresponding line. read ignores leading tabs making it impossible to use if we care about which columns had the missing elements.

In Bash (or some other shell that supports $'' and arrays), this should do:

IFS=$'\t' paste arg1.txt arg2.txt | while read -r -a A ; do [ "${#A[@]}" -eq 2 ] && printf "%s - %s\n" ${A[@]} done 

paste sticks the input files together line-by-line, read -a A reads the columns to the array A, using tab (from IFS) as a separator. [ "${#A[@]}" -eq 2 ] checks that the array has exactly two members, and ${A[@]} drops them on the command line of printf. Change the command as required.

With these input files:

$ cat arg1.txt foo bar doo $ cat arg2.txt one two two three three three 

The output from the above snippet is:

foo bar - one doo - two two 

The last line from arg2.txt is ignored, since arg1.txt doesn't have a corresponding line. read ignores leading tabs making it impossible to use if we care about which columns had the missing elements.

In Bash (or some other shell that supports $'' and arrays), this should do, as long your input files don't contain tabs(*):

IFS=$'\t' paste arg1.txt arg2.txt | while read -r -a A ; do [ "${#A[@]}" -eq 2 ] && printf "%s - %s\n" ${A[@]} done 

paste sticks the input files together line-by-line, read -a A reads the columns to the array A, using tab (from IFS) as a separator. [ "${#A[@]}" -eq 2 ] checks that the array has exactly two members, and ${A[@]} drops them on the command line of printf. Change the command as required.

(* if you need to support tabs, I'd move to using e.g. Perl)

With these input files:

$ cat arg1.txt foo bar doo $ cat arg2.txt one two two three three three 

The output from the above snippet is:

foo bar - one doo - two two 

The last line from arg2.txt is ignored, since arg1.txt doesn't have a corresponding line. read ignores leading tabs making it impossible to use if we care about which columns had the missing elements.

Source Link
ilkkachu
  • 148.2k
  • 16
  • 268
  • 441

In Bash (or some other shell that supports $'' and arrays), this should do:

IFS=$'\t' paste arg1.txt arg2.txt | while read -r -a A ; do [ "${#A[@]}" -eq 2 ] && printf "%s - %s\n" ${A[@]} done 

paste sticks the input files together line-by-line, read -a A reads the columns to the array A, using tab (from IFS) as a separator. [ "${#A[@]}" -eq 2 ] checks that the array has exactly two members, and ${A[@]} drops them on the command line of printf. Change the command as required.

With these input files:

$ cat arg1.txt foo bar doo $ cat arg2.txt one two two three three three 

The output from the above snippet is:

foo bar - one doo - two two 

The last line from arg2.txt is ignored, since arg1.txt doesn't have a corresponding line. read ignores leading tabs making it impossible to use if we care about which columns had the missing elements.