Skip to main content
Made the sample file content match its description (\n newlines)
Source Link
fra-san
  • 10.9k
  • 2
  • 27
  • 45

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n''aa\nbb\ncc\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; delimiter though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; delimiter though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\nbb\ncc\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; delimiter though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 
added missing space in explanation re delimiter. (The word "delimiter" has just been added b/c of the 6 character edit minimum)
Source Link

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; delimiter though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; delimiter though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 
added 234 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

Just use perl. Sed is more complicated to use with newlines, but perl can handle them easily:

printf 'aa\r\nbb\r\ncc\r\n' > file printf 'aa2\r\nbb2\r\ncc2\r\n' > file2 printf 'aa3\rbb3\rcc3\r' > file3 

So, file has \n line endings, file2 has \r\n and file3 has \r (which is obsolete these days, by the way, not much point in supporting it). Now, concatenate them into a string:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3; 

You'll need a second pass to remove the trailing ; though:

$ joined_string_var=$(perl -pe 's/(\r\n|\r|\n)/; /g' file file2 file3 | sed 's/; $//') $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 

Or, remove it in perl:

$ joined_string_var=$(perl -ne 's/(\r\n|\r|\n)/; /g; $k.=$_; END{$k=~s/; $//; print $k}' file file2 file3) $ echo "$joined_string_var" aa; bb; cc; aa2; bb2; cc2; aa3; bb3; cc3 
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719
Loading