Help in preserving special characters from input file

Hi Forum.

I've tried to search online for a solution but I cannot seem to find one.

Hopefully, someone here can help me out. I would appreciate it.

Input file abc.txt:

[S\_M\_F\_ING\_MTG\_COM\_CUSTOMER_EXTRACT]
$InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst
$InputFile_Customer=CMTSLST\EDW_COMMERCIAL_MTGE_CUSTOMER_dat.lst
$OuputFile_BorrowerNoGridID=CMTS\cm_borrower_exception_no_grid_id.out
$OutputFile_BorrowerDup=CMTS\cm_borrower_exception_dup.out

Output from script:
[S\_M\_F\_ING\_MTG\_COM\_CUSTOMER_EXTRACT]
$InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst
$InputFile_Customer=CMTSLST\EDW_COMMERCIAL_MTGE_CUSTOMER_dat.lst
$OuputFile_BorrowerNoGridID=CMTS$OutputFile_BorrowerDup=CMTS

And here's the simple sample script to read/output the input file:

cat abc.txt | while read -r line
do
echo $line
done

The read "-r" option is suppose to treat the input data as raw, but I think the script is trying to interpret the line $OuputFile_BorrowerNoGridID=CMTS\cm_borrower_exception_no_grid_id.out
.... as \c special character.

I've read that single quotes can preserve the input data as is with no interpretation but I'm not sure how to implement it in my above script.

Thanks.

Do you have only 5 lines in your file and do you want to discard the last line?
Please elaborate your question.

yes well, the trouble is the echo.

what exactly are you trying to achieve?

Sorry if I wasn't clear.

I'm trying to preserve the data from the input file As is.

Basically, if I was going to output the script to another file def.txt, then
file abc.txt should be identical to def.txt

I don't want my script to interpret any special characters from the input file.

Thank you.

So the files abc.txt and def.txt should be identical?

Why don't you use the cp command?

Homework?

What shell ? , works for me in bash(see below)

# cat file
[S_M_F_ING_MTG_COM_CUSTOMER_EXTRACT]
$InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst
$InputFile_Customer=CMTSLST\EDW_COMMERCIAL_MTGE_CUSTOMER_dat.lst
$OuputFile_BorrowerNoGridID=CMTS\cm_borrower_exception_no_grid_id.out
$OutputFile_BorrowerDup=CMTS\cm_borrower_exception_dup.out

# while read -r line;do echo $line;done < file
[S_M_F_ING_MTG_COM_CUSTOMER_EXTRACT]
$InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst
$InputFile_Customer=CMTSLST\EDW_COMMERCIAL_MTGE_CUSTOMER_dat.lst
$OuputFile_BorrowerNoGridID=CMTS\cm_borrower_exception_no_grid_id.out
$OutputFile_BorrowerDup=CMTS\cm_borrower_exception_dup.out

Probably his shell/OS is using an echo statement that interprets escape sequences by default.

Try "print -r" rather than "echo" to preserve "\c". And put $line in quotes.

cat abc.txt | while read -r line
do
        print -r "${line}"
done

[S_M_F_ING_MTG_COM_CUSTOMER_EXTRACT]
$InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst
$InputFile_Customer=CMTSLST\EDW_COMMERCIAL_MTGE_CUSTOMER_dat.lst
$OuputFile_BorrowerNoGridID=CMTS\cm_borrower_exception_no_grid_id.out
$OutputFile_BorrowerDup=CMTS\cm_borrower_exception_dup.out

Thank you Methyl......your solution works great!!!!

Is there a way to store the value in a variable? I tried the following but it's not working as expected:

cat abc.txt | while read -r line
do
var1=`print -r "${line}"`
echo "$var1"
done

The reason that I want to store the value in a variable is because I have some logic in my script and want to make reference use of a variable var1 in a number of places.

This required solution is work-related and script is doing some logic verification based on input file and it's not a matter of just copying (cp) the input file.

I've only shown a simplified sample script in order to get a solution.

Remember that "echo" reacts to "\c" so we need to avoid echo.
If you need the variable in $var1 we don't need $line .

cat abc.txt | while read -r var1
do
       print -r "${var1}"
done

And to top it off, the pipe can be left out as well and IFS= preserves leading spacing.

while IFS= read -r var1
do
   print -r "$var1"
done < abc.txt

One observation. Is it safe to assume that the file names in your variables are for a Windows or Netware server? Filenames containing a backslash character "\" will give you grief in a unix filesystem and as you have found require special treatment in shell variables. The unix delimiter between a directory name and a file name is of course a solidus character "/".

I'm processing an input file containing this data:

[S\_M\_F\_ING\_MTG\_COM\_CUSTOMER_EXTRACT]
$InputFile_Borrower=CMTSLST\EDW_COMMERCIAL_MTGE_BORROWER_dat.lst
$InputFile_Customer=CMTSLST\EDW_COMMERCIAL_MTGE_CUSTOMER_dat.lst
$OuputFile_BorrowerNoGridID=CMTS\cm_borrower_exception_no_grid_id.out
$OutputFile_BorrowerDup=CMTS\cm_borrower_exception_dup.out

The text in bold-faced-red is making reference to a subdirectory on our Unix server.

From what I understand, the input data cannot be stored in a variable called var1? I wanted to store the input data in a variable so that I can compare it to some constant value (while also preserving the input raw data).

For ex: Logic in my script is as follows:

if $var1 = "ABC" then
output_line = "First level"
else
if $var1 = "CDE" then
output_line = "Second level"
else
output_line = "Third level"
fi
fi

As you can see, it's not just reading the input and printing it out - I need to process the input data and comparing it.

Thanks.

That is not the correct format for a unix directory.

yes - I realize that.

I will change it to the correct format (/) so that my issue is resolved.

Thanks for all your help.