Removing "^M" from the end of a String (i.e. "Ctrl+M")?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing "^M" from the end of a String (i.e. "Ctrl+M")?
# 1  
Old 07-25-2012
Removing "^M" from the end of a String (i.e. "Ctrl+M")?

Hello All,

I have an Expect script that ssh's to a remote server and runs some commands before exiting.
One of the commands I run is the "hostname" Command. After I run this command I save the output
using this line in the code below...

Basically it executes the hostname command, then I loop through the "$expect_out(buffer)" variable
line by line. The "$expect_out(buffer)" variable holds the output of the last "send" command, up to
the next "expected" ouput.
Code:
....
send -- "hostname\r"
expect {
    -re "^hostname.*\r.*" {
        set found 0
        foreach line [split $expect_out(buffer) "\n"] {
            if {$found == 1} {
                set host_name "$line"
                break
            }
            if {[regexp ".*hostname.*" $line]} {
                set found 1
                continue
            }
        }
        puts -nonewline "host_name = \"$host_name\"\n"
    timeout { exit 1 }
    default { exit 11 }
}

The reason I know that it's showing a "^M" at the end of that variable, is because I redirected the output
to a file on one of my tries, and while viewing the output in vi I was able to see the special characters.

And this is the output from the "puts" command. It shows like this below (notice the quotation mark is begin
moved to the start of that line.)...
Code:
"ost_name = "My-VM-Host

As you can see the last quotation mark is replacing the first character on the same line...

Any thoughts how I could get rid of this "^M" from my variable..? It would be VERY much appreciated!


Thanks in Advance,
Matt
# 2  
Old 07-25-2012
Try:

Code:
regsub -all "\cM" $host_name "" host_name

# 3  
Old 07-26-2012
Hey Chubler_XL, thanks for the reply.

I gave that a shot and I still am getting the same result...
I also switched out your "pattern" with "\^M" and "\\^M" and still no luck.

I ran my Expect Script with the tee command to check out the real output. And if I open the file created from the tee command
I can see this line for the output for the Hostname.
Code:
Hostname = "suse-vm-host1^M"

Now if you view the output in STDOUT it looks like this (F.Y.I. I printed it with escaped parentheses to view the start/ending points of the variable)...
Code:
"ostname = "suse-vm-host

But anyway, thanks for the "regsub" command though. I've been looking for a command like that.


Thanks Again,
Matt

---------- Post updated at 09:58 AM ---------- Previous update was at 09:53 AM ----------

UPDATE:

Humm... Looks like the "^M" is actually the same thing as a carriage return --> "\r"...
So I switched out the pattern to match with the "\r" and it seems to be working..!!!
Code:
regsub -all "\r" $host_name "" host_name

And the output looks good, both in STDOUT and in the file fro the tee command.


Thanks for the regsub command, exactly what I needed! I appreciate your reply.


Thanks Again,
Matt
This User Gave Thanks to mrm5102 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

7. Shell Programming and Scripting

Purpose of "read" and "$END$" in ksh ?

Hi, Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator? cat $AMS/version|read a b verno d DBVer=$(/usr/bin/printf "%7s" $verno) I checked that the cat $AMS/version command returns following output: ... (10 Replies)
Discussion started by: dbadmin100
10 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question