Echo is replacing characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo is replacing characters
# 1  
Old 02-23-2012
Echo is replacing characters

Hi All,
I'm using KSH and am writing a small script that counts the lines of various files in several folders under one root folder.

Below is the script:

Code:
 
#!/usr/bin/ksh
common_path_in="/interface_in/rsc"
file_out="/interface_in/rsc/record_count.csv"
tot_rec_count=-1
act_rec_count=-1
echo ${common_path_in}
echo ${file_out}
echo ${tot_rec_count}
echo ${act_rec_count}
set -A folders horizon mysteryshopper rcc reference
echo ${folders[@]}
for i in ${folders[@]}
do echo ${common_path_in}
echo "${common_path_in}/$i"
echo `ls -1 ${common_path_in}/$i/*.csv`;done

Please find the output screenshot attached for your reference. Following are my questions regarding the output that I'm getting -

1. The output of the line marked in red is the problem:
common_path_in="/interface_in/rsc"
$i in first iteration is - ${folders[0]}=horizon
so I should get "/interface_in/rsc/horizon", but I'm getting "/horizonce/rsc/". horizon is coming at the beginning of the string.

2. Also I've noticed that, if I give line breaks before the loop, then I'm getting some error - 'unexpected `do' ' or something like this. Even I'm getting this error if I place 'do' and 'done' in a separate lines alone.

Could anyone of you please look into it and let me know where I'm doing wrong?

Best Regards,
Jagari
Echo is replacing characters-echo_errorpng
# 2  
Old 02-23-2012
You've probably edited the script with a Microsoft editor such as Notepad.

Please post the output from this "sed" command which is designed to make control characters visible. A normal unix text file line terminator will be displayed as a dollar sign. One which has been edited with a Microsoft editor will also have carriage-return characters displayed as "\r".

Code:
sed -n l scriptname

Where "scriptname" is the name of your script file.
This User Gave Thanks to methyl For This Post:
# 3  
Old 02-23-2012
Hi Methyl,

Yes, I was editing the script in notepad/wordpad.
Please find attached the screenshot of 'sed' that you've suggested.

I'm pasting the output below:

Code:
 
#!/usr/bin/ksh\r$
common_path_in="/interface_in/rsc"\r$
file_out="/interface_in/rsc/record_count.csv"\r$
tot_rec_count=-1\r$
act_rec_count=-1\r$
echo $common_path_in\r$
echo $file_out\r$
echo $tot_rec_count\r$
echo $act_rec_count\r$
set -A folders horizon mysteryshopper rcc reference\r$
echo ${folders[@]}\r$
for i in ${folders[@]}\r$
do echo $common_path_in\r$
echo $i\r$
name=${common_path_in}/$i\r$
echo $name\r$
echo `ls -1 $common_path_in/$i/*.csv`;done$

Thanks,
Jagari
# 4  
Old 02-23-2012
Quote:
for i in ${folders[@]}
do echo ${common_path_in}
echo "${common_path_in}/$i"
echo `ls -1 ${common_path_in}/$i/*.csv`;done
Should be:
Code:
for i in ${folders[@]}
do
    echo ${common_path_in}
    echo "${common_path_in}/$i"
    echo `ls -1 ${common_path_in}/$i/*.csv`
done

# 5  
Old 02-23-2012
To fix your script we need to remove those carriage-returns:

Code:
cat oldscript | tr -d '\r' > newscript
# Then make new script executable
chmod 755 newscript

Obviously you sould be using a unix editor.
# 6  
Old 02-23-2012
Hi Methyl,

Now I've edited the script in vi and have done this identation change that you've suggested.

I'm getting the below output and error message -

/interface_in/rsc
/interface_in/rsc/record_count.csv
-1
-1
horizon mysteryshopper rcc reference
test.ksh: line 10: syntax error at line 17: `for' unmatched

Intially I was following indentation and line breaks, but I was getting this same error. When I remove all extra spaces and indentation then this error was not there.

Please advise.

Regards,
Jagari

---------- Post updated at 11:58 AM ---------- Previous update was at 11:57 AM ----------

okay, let me try this... getting back to you after changing it...

---------- Post updated at 12:03 PM ---------- Previous update was at 11:58 AM ----------

great!!! It's working now Smilie
thanks a lot Methyl.

Warm Regards,
Jagari
This User Gave Thanks to jagari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing echo with print

Hi everyone, I'm executing a shell script and one of the commands is creating a file with text via echo. However, if the text within echo has "\t" or similar, it automatically translates it into a TAB character, same goes for other special characters. I know that if I put another "\"... (7 Replies)
Discussion started by: demmel
7 Replies

2. Shell Programming and Scripting

Replacing part of the sentence using echo and sed

Hi, Iam using ksh and trying to execute the following syntax to replace one word of the sentence with a new word. But somehow sed is not able to replace the old value with new value. Please let me know where Iam going wrong. Sample Code : --> export line="VORTEX,abcdef" export... (3 Replies)
Discussion started by: ajithab
3 Replies

3. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

4. Shell Programming and Scripting

Replacing characters

Hi fellow experts, I have a question for you. Data looks like: 00877,05/13/2010,PBO,P,0000708331,518 00877,05/13/2010,PBO,P,0000708331,519 ... ... 00877,05/13/2010,PBO,P,0000708331,2103 00877,05/13/2010,PBO,P,0000708331,2104,etc,etc Basically I have to replace 518,519,2103,2104,... (4 Replies)
Discussion started by: Devski123
4 Replies

5. Shell Programming and Scripting

exclude characters in echo command please help

Hi all, I wrote one code and i face some difficulties to exclude some characters from the echo command more specifically the last command is echo $a "${RESULT}" >> results_data_srcip the results which i have taken are: nfcapd.200908250000 -.352228 nfcapd.200908250005 -.345085 ... (4 Replies)
Discussion started by: draxmas
4 Replies

6. Shell Programming and Scripting

Replacing Characters with |

Hi All, example data.log 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 I want like this to 526569346|66815531961|09 526569346|66815531961|09... (4 Replies)
Discussion started by: ooilinlove
4 Replies

7. Shell Programming and Scripting

Replacing Characters

Hi All, I have a file which is delimeted with the character '. i need to replace this character with the same character and also a new line. Can anyone please help me with the tr command for this. Many thanks Karan (11 Replies)
Discussion started by: karansachdeva
11 Replies

8. Shell Programming and Scripting

replacing characters

hi all I have a file that has sone spaces in start then / at last. i want to get rid of this. how to do? eg. 11414/ 49878/ 27627/ I WANT THE FILE AS 11414 49878 27627 PLEASE HELP (3 Replies)
Discussion started by: infyanurag
3 Replies

9. UNIX for Dummies Questions & Answers

replacing characters

Hi, I have a script for replacing bad characters in filenames for f in *; do mv $f `echo $f | tr '+' '_'` done; this replaces + for _ But I need to replace all bad characters ? / % + to _ Pls how can i do this in one script ? (3 Replies)
Discussion started by: palmer18
3 Replies

10. UNIX for Dummies Questions & Answers

How to echo password characters

Hi All, I was wondering, how can I echo password using a character such as '*' in unix? In my machine, password is not echoed, so it is only blank space when I enter any password. So, when I enter a command such as "lock", how can I set the echo character to "*" or something else for... (4 Replies)
Discussion started by: mukluk
4 Replies
Login or Register to Ask a Question