Help in preserving special characters from input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in preserving special characters from input file
# 8  
Old 01-29-2010
Try "print -r" rather than "echo" to preserve "\c". And put $line in quotes.

Code:
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

# 9  
Old 01-29-2010
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.
# 10  
Old 01-29-2010
Remember that "echo" reacts to "\c" so we need to avoid echo.
If you need the variable in $var1 we don't need $line .

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

# 11  
Old 01-29-2010
And to top it off, the pipe can be left out as well and IFS= preserves leading spacing.
Code:
while IFS= read -r var1
do
   print -r "$var1"
done < abc.txt

# 12  
Old 01-29-2010
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 "/".
# 13  
Old 01-29-2010
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.
# 14  
Old 01-29-2010
Quote:
The text in bold-faced-red is making reference to a subdirectory on our Unix server.
That is not the correct format for a unix directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename File Name with Special Characters

I am trying to rename files with spaces and other characters and not able to be successful. FileNames: UPLOAD REFERENCE.xls UPLOAD MASS REFERENCE.XLS find /UPLOAD REFERENCE/ -depth -type f -name "* *" -exec rename " " "_" "{}" ";" The above one is successful to replace spaces... (1 Reply)
Discussion started by: eskay
1 Replies

2. Shell Programming and Scripting

How to delimit the fields of a input file which has special characters?

Hi All, I am a newbie to Shell scripting. I have a requirement to Delimit the file fields of a Input file having special characters and spaces with ";". Input File ---------------------------------- Server Port ---------------------------------- Local ... (5 Replies)
Discussion started by: Suganbabu
5 Replies

3. Red Hat

Special control characters in file

Hi Guys, We receive some huge files on to Linux server. Source system use FTP mechanism to transfer these files on our server. Occasionally one record is getting corrupted while transfer, some control characters are injecting into the file. How to fix this issue ? please advice ? Sample... (2 Replies)
Discussion started by: srikanth38
2 Replies

4. Shell Programming and Scripting

File containing special characters

Hello All, I am facing challenges in order to transfer a file from windows to unix box,the file contains a special character '×' ,now when I am transferring the file from windows to unix that special character converted to something else like 'Ã' ,another thing I have noticed that the hardware is... (1 Reply)
Discussion started by: prarat
1 Replies

5. Shell Programming and Scripting

Check input for lenght, special characters and letter case

I made menu script for users so they can run other script without going in shell just from menu. But i must control their input. These are criteria: Input must have 4 signs First two signs are always lower case letters Input shall not have some special signs just letters and numbers ... (1 Reply)
Discussion started by: waso
1 Replies

6. Shell Programming and Scripting

Ommiting special characters as input - Help

Hey Everyone, I'm quite new to unix (hence the 0 posts!) and im trying to write a simple program that outputs what the user types in to the screen, as long as it is a letter. This part works fine, however, when a "\" is entered doesnt not display anything and moves to the next line. Is... (11 Replies)
Discussion started by: ultiron
11 Replies

7. Shell Programming and Scripting

Bourne Shell: Special characters Input Prevention

Environment: Sun UNIX Language: Bourne Shell Problem: I am writing a script that allows user to key in a directory. Example: /root/tmp. Since the user can key in anything he/she wants, I need to validate to make sure that he/she does not key in anything funny i.e.... (37 Replies)
Discussion started by: totziens
37 Replies

8. UNIX for Dummies Questions & Answers

How to delete a file with special characters

I don't now exactly how I did it, but I created a file named " -C " cexdi:/home1 $ls -lt total 1801336 -rw------- 1 cexdi ced-group 922275840 23 mars 10:03 -C How do I delete this file ? cexdi:/home1 $rm -C rm: invalid option -- C Syntax : rm filename ... Doesn't work...... (5 Replies)
Discussion started by: yveslagace
5 Replies

9. UNIX for Dummies Questions & Answers

Replace Special characters in a file

Hi, I have a data like this in a file, 402003279034002000100147626030003300010000000000002000029000000 ær^M^\MÍW^H I need to replace those special char to some other char like # or $ Is there any ways to do it... I tried commands tr,sed and many but it was not able to replace because... (1 Reply)
Discussion started by: solai
1 Replies

10. UNIX for Dummies Questions & Answers

how to see special characters in a file using vi

Hi, I have a file which has special characters. I can't see them when I "vi" the file. But I am sure there are some special un seen characters. How can I see them? Please help. Thx (6 Replies)
Discussion started by: jingi1234
6 Replies
Login or Register to Ask a Question