Skip to main content
Update answer according to feedback from OP
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

Since using shell loops for text processing is considered bad practice, I would recommend a different approach that uses dedicated text-processing tools. In particular, awk comes to mind:

awk '{printf "text %s text\n",$0}' textfile.txt 

This will use Awk's builtin printf function (using the same formatting syntax as the well-known C function) to print the entire current line (represented by $0) enclosed by text on both ends.

As for why your approach didn't work, it can beAs for why your approach didn't work: Since you confirmed that if the text file was edited on a Windows-based machine, this means that the line endings are in a format the Linux doesn't correctly interpret. In that case, run the file through dos2unix, as in

dos2unix textfile.txt

The resulting file should be processed correctly.

Since using shell loops for text processing is considered bad practice, I would recommend a different approach that uses dedicated text-processing tools. In particular, awk comes to mind:

awk '{printf "text %s text\n",$0}' textfile.txt 

This will use Awk's builtin printf function (using the same formatting syntax as the well-known C function) to print the entire current line (represented by $0) enclosed by text on both ends.

As for why your approach didn't work, it can be that if the text file was edited on a Windows-based machine, the line endings are in a format the Linux doesn't correctly interpret. In that case, run the file through dos2unix, as in

dos2unix textfile.txt

Since using shell loops for text processing is considered bad practice, I would recommend a different approach that uses dedicated text-processing tools. In particular, awk comes to mind:

awk '{printf "text %s text\n",$0}' textfile.txt 

This will use Awk's builtin printf function (using the same formatting syntax as the well-known C function) to print the entire current line (represented by $0) enclosed by text on both ends.

As for why your approach didn't work: Since you confirmed that the text file was edited on a Windows-based machine, this means that the line endings are in a format the Linux doesn't correctly interpret. In that case, run the file through dos2unix, as in

dos2unix textfile.txt

The resulting file should be processed correctly.

Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

Since using shell loops for text processing is considered bad practice, I would recommend a different approach that uses dedicated text-processing tools. In particular, awk comes to mind:

awk '{printf "text %s text\n",$0}' textfile.txt 

This will use Awk's builtin printf function (using the same formatting syntax as the well-known C function) to print the entire current line (represented by $0) enclosed by text on both ends.

As for why your approach didn't work, it can be that if the text file was edited on a Windows-based machine, the line endings are in a format the Linux doesn't correctly interpret. In that case, run the file through dos2unix, as in

dos2unix textfile.txt