Script with ^M causes display issue with cat or more


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script with ^M causes display issue with cat or more
# 1  
Old 05-09-2013
Script with ^M causes display issue with cat or more

I have a script to do a couple simple but repetitive commands on files that are provided to us. One of the things is to get rid of the line feeds. This is the section that is causing problems, i even cut this section into its own file to make sure nothing else was affecting it.
Code:
#!/usr/bin/bash
ls File_Name_* |while read FN
     do
          sed 's/^M//' ${FN} > ${FN}.tmp     ### the ^M is input with ^v^M
          mv ${FN}.tmp ${FN}
     done

It works fine and shows up fine in vi. but if i try to cat or more the file it shows
Code:
#!/usr/bin/bash
ls File_Name_* |while read FN
do
//' ${FN} > ${FN}.tmp/
                       mv ${FN}.tmp ${FN}
     done

Oh if i usr /usr/xpg4/bin/more it shows it all except the ^M itself:
Code:
#!/usr/bin/bash
ls File_Name_* |while read FN
     do
          sed 's///' ${FN} > ${FN}.tmp     ### the ^M is input with ^v^M
          mv ${FN}.tmp ${FN}
     done

Can someone explain this. The ^M is affecting the display of the file and i have never seen this before. This is on a Solaris 9 system.
# 2  
Old 05-09-2013
1st query is working fine for me.
Would be great if you could show the output you are getting... I tried with vi, cat and more, I could see no ^M characters in the output files.
Or am I missing something?
# 3  
Old 05-09-2013
^M stands for CarriageReturn (CR). Carriage return means go to the beginning of the line. So in the case of the cat, it writes:
Code:
          sed 's/

then it encounters the CR so it returns to the beginning of the line and prints:
Code:
//' ${FN} > ${FN}.tmp

meanwhile overwriting
Code:
          sed 's/

# 4  
Old 05-13-2013
I did understand that the ^M is a carriage return, I didn't expect it to work from withing the '^M' quotes, especially single quotes.
# 5  
Old 05-13-2013
What do quotes matter to cat, or the terminal itself? ^M is a terminal control character. If you print it, the cursor goes to the beginning of the line.

It may be an option for you to do something like
Code:
VAR=$'\r'

or

Code:
VAR="$(printf "\r")"

to avoid having to embed a literal carriage return in your script.

Last edited by Corona688; 05-13-2013 at 06:06 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with cat command on a for loop

Good day to all, I'd like to ask for your advice with regards to this. Scenario : I have here a file named TEST.tmp wherein the value inside is below; "ONE|TWO|FIVE|THREE|FOUR|SIX~SEVEN~EIGHT" "NINE" But when I'm trying to use this in a simple command like; for TESTING in $(cat... (4 Replies)
Discussion started by: asdfghjkl
4 Replies

2. Shell Programming and Scripting

Cat command not working to display Mac file in Ubuntu

Hi, Recently I got a .txt file from Mac user. when I try to open it in my Ubuntu machine using cat command it is not displaying any content of file however I can see the content using vi. Anyone know How to see its content using cat as I have to process it in my shell script. Thanks in... (4 Replies)
Discussion started by: diehard
4 Replies

3. Shell Programming and Scripting

Issue in cat command

We have shell script (in ksh) which reads the records from a csv file line by line and does some operation. We have below command to read the file (CSV has the absolute path of files stored on a server something like SER/IMP/TEST/2010/12/123465.1). P_FILES=`cat $P_BATCH_FILE` for i in $P_FILES... (2 Replies)
Discussion started by: shreevatsau
2 Replies

4. Shell Programming and Scripting

simple for loop/cat issue

ok.. so problem is: I have a file that reads: cat 123 1 and 2 3 and 4 5 and 6 I was using for loops to run through this information. Code: for i in `cat 123` do echo $i done shouldn't the output come as 1 and 2 (3 Replies)
Discussion started by: foal_11
3 Replies

5. Emergency UNIX and Linux Support

cat issue

I have list of files in my current directory abc.txt 123.csv 234.csv 245.csv 145.csv 123_ex_c.sv I don't want to open first and last file. i.e (abc.txt and 123_ex_csv) I tried cat *.csv, but it won't work. Can anyone tell me the proper regex only in cat Thanks Pritish ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

6. Shell Programming and Scripting

Display variables in CAT area

Hi All, I've got a script to output YAML data, and I want to display data that's held inside variables inside one large CAT area. What's the easiest way to do this? cat << "END" --- classes: - general_image - $intro #Variable 1 - $mid #Variable 2 ... (2 Replies)
Discussion started by: glarizza
2 Replies

7. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

8. Shell Programming and Scripting

Issue with Unix cat command

Hi Experts, I am finding the performance of cat command is very wierd, it is taking more time to merge the files into a single file. We have a situation where we would be merging more than 100 files into a single file, but with cat command it is running slow. I tried doing with paste, join... (13 Replies)
Discussion started by: RcR
13 Replies

9. UNIX for Dummies Questions & Answers

How to Display a range of values in the output of cat

When I use this command I get an output of some numbers cat ac.20070511 | cut -d" " -f19 Is there any way for me to display only the numbers that are greater than 1000 but not all the numbers in the ouput. Can any one help me with this. :) (8 Replies)
Discussion started by: venu_nbk
8 Replies

10. UNIX for Dummies Questions & Answers

using cat and grep to display missing records

Gentle Unix users, Can someone tell me how I can use a combination of the cat and grep command to display records that are in FileA but missing in FileB. cat FileA one line at a time and grep to see if it is in fileB. If it is ignore. If line is not in fileB display the line. Thanks in... (4 Replies)
Discussion started by: jxh461
4 Replies
Login or Register to Ask a Question