sed to delete ^M


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to delete ^M
# 1  
Old 11-20-2014
sed to delete ^M

I am trying to use sed to delete the ^M at the end of the line and when i pass a file './asciiFix.sh wintest.txt' I get an error saying sed: can't read : no such file or dir. Can someone give me a pointer as to what I am doing wrong?

Code:
#!/bin/sh
if file "$@" | grep "with CRLF"; then
   echo "converting $file"
   sed -i 's/^M//g' $file
fi

# 2  
Old 11-20-2014
$file is never defined.

You may also want to consider the dos2unix (or dos2ux) command instead of your script. It will process a text and leave it alone if it does not have windows carriage control.

How did you write the ^M ? It actually should be ascii 13 not the ^ character and the letter M.
# 3  
Old 11-20-2014
My system does not have dos2unix installed on it and the admin has it to where it cannot be installed. ctrl+v+m is how I got
Code:
^M

. I was thinking of trying
Code:
's/\r$//'

in place to see and it worked on some files, but not all. I have changed it though to where my $file is defined.
# 4  
Old 11-21-2014
If your files happen to have <CR> line terminators, could it be the filenames have it as well?
# 5  
Old 11-21-2014
Like Jim says, your script does not put the filename of the file in the variable file, so $file will be empty.
# 6  
Old 11-21-2014
Have a for loop around it that defines a $file for each parameter
Code:
for file
do
 if file "$file" | grep "with CRLF"; then
  echo "converting $file"
  sed -i 's/^M//g' "$file"
 fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed delete

I am not able to analyze the below code.. What I expected is that DELETED will be replaced with the first field in every line.But after giving a try, the output was different. Can anyone pleas analyze and explain the code in detail? Many thanks... $ sed 's/* /DELETED /g' g1.txt > g4.txt... (2 Replies)
Discussion started by: giridhar276
2 Replies

2. Shell Programming and Scripting

sed delete range

Hi I would like to delete ranges of text from an html file; In the sentence; aqua>Stroomprobleem in Hengelo verholpen <a href="107-01.html"><font color=yellow>107</a> With several sentences like this in that file, where the text between <a href a> varies, so it needs to be deleted in the... (2 Replies)
Discussion started by: mdop
2 Replies

3. Shell Programming and Scripting

sed delete option

I have tried doing this to delete some lines: sed '1,10d' file Now I want to specify a variable as a line number for example: lastline=wc -l file linestart=$lastline - 20 sed '$linestart,$lastlined' file but this will give error: sed: -e expression #1, char 3: extra characters after... (4 Replies)
Discussion started by: zorrox
4 Replies

4. Shell Programming and Scripting

sed - delete everything until the first digit comes up

Hi, I have a text file with content as follows: bla foo3200492 comment on this: 3900302 here comes the teddy 12 all I need is: 3200492 3900302 12 So I need the correct "sed" command to delete everything until the first number. I am not a regex expert, because I have to... (3 Replies)
Discussion started by: mcW
3 Replies

5. Shell Programming and Scripting

sed-delete

Guys, file1: test \ 123 cat file1 | sed '/test \/d' ---- does not work I have to remove the line "test \" in file1 can somebody help ? (7 Replies)
Discussion started by: giri_luck
7 Replies

6. Shell Programming and Scripting

delete using sed

hi , i need to delete all the lines in a file except a line which starts with the letter SP (6 Replies)
Discussion started by: mhdmehraj
6 Replies

7. UNIX for Dummies Questions & Answers

Delete ^ using sed

Hi All, I am very new to UNIX... I was trying to delete ^ from a file and save it with different name... The version is AIX 5.3 The input file is in this directory /a/b The Input file name is o9876.out I want the output file in same path with different name like ABC.txt... (5 Replies)
Discussion started by: us_pokiri
5 Replies

8. UNIX for Dummies Questions & Answers

Sed: Append and Delete?

I think it should be obvious what I'm trying to do from my command, but just to be sure it's clear, I'm trying to append 2 lines after a matched line and then delete 2 other lines within a file. I'd like to do it in 1 line if possible. This code isn't working, but hopefully it's just a syntax... (0 Replies)
Discussion started by: earnstaf
0 Replies

9. Shell Programming and Scripting

sed help - delete last 2 lines.

I have been reading through the sed one liners, trying to understand what is happening. # delete the last 2 lines of a file sed 'N;$!P;$!D;$d' The above will delete the last 2 line of a file. I tried analyzing what happens. And I got lost :( This is what I understood so far from the... (2 Replies)
Discussion started by: vino
2 Replies

10. UNIX for Dummies Questions & Answers

Using sed to delete a string?

Hi all! Here is my problem : I have a string like the following : 20030613170404;BAN_CAV ; starting script Loader.sh on ; 13/06/2003 at ; 17;04;03 I want to eraze all characters located after "Loader.sh", because there are unuseful. I tried to use sed...but it didnt work....i guess i... (1 Reply)
Discussion started by: HowardIsHigh
1 Replies
Login or Register to Ask a Question