Search and Delete pattern with Control M


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Search and Delete pattern with Control M
# 1  
Old 06-26-2017
Search and Delete pattern with Control M

Hi
I have data file like below:

Code:
NET_SALES^M  ^M---- new fields for  --  ^M  ,YABA_FLAG^M  ,DOO_FLAG^M

My requirement is to search for atleast 2 -- and remove all the words till first ^M is encountered.
In above case, output should be like

Code:
NET_SALES^M  ^M ,YABA_FLAG^M  ,DOO_FLAG^M

TIA
Nitin

Last edited by dashing201; 06-26-2017 at 06:53 AM..
# 2  
Old 06-26-2017
Code:
sed 's/--[^\r]*\r//g' file

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 06-26-2017
Hi Andrew

Thanks for quick reply.
However I am not getting proper result

Code:
echo "NET_SALES^M  ^M---- new fields for  --  ^M  ,YABA_FLAG^M  ,DOO_FLAG^M"|sed 's/--[^\r]*\r//g'

Output:
Code:
  ,DOO_FLAGG

Expect Output:
Code:
NET_SALES^M  ^M^M  ,YABA_FLAG^M  ,DOO_FLAG^M

---------- Post updated at 04:24 PM ---------- Previous update was at 04:21 PM ----------

Surprisingly , it worked like charm on data file.
Thanks a lot.Smilie
# 4  
Old 06-26-2017
The problem is that your output terminal is translating the literal ^M as carriage returns and the line is being overwritten. Try sending the output to a file and reading it in an editor which will show the carriage returns, or try (for the sake of testing but only for testing) adding
Code:
 | sed 's/\r/^M/g'

to the above line. Or even
Code:
| cat -vet

Andrew
# 5  
Old 06-26-2017
^M is usually a leftover from writing a file in a windows text editor, then copying/writing it to UNIX.
If you are processing the data for a file that gets loaded into another application or a database, you may not want ^M characters in the final file product.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

How to delete all lines before a particular pattern when the pattern is defined in a variable?

I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ... (21 Replies)
Discussion started by: Soham
21 Replies

3. UNIX for Dummies Questions & Answers

How to delete blank line/s before and after a search pattern?

Hi, Test file x.txt below. This file is generated by a program that I unfortunately do not have control on how it gets presented/generated. create PACKAGE "XXX_INTERFACE_DEFECT_RPT_TEST" is TYPE refCursor IS REF CURSOR; Function queryRecords ( p_status varchar2, ... ... ... )... (4 Replies)
Discussion started by: newbie_01
4 Replies

4. Shell Programming and Scripting

Search for duplicates and delete but remain the first one based on a specific pattern

Hi all, I have been trying to delete duplicates based on a certain pattern but failed to make it works. There are more than 1 pattern which are duplicated but i just want to remove 1 pattern only and remain the rest. I cannot use awk '!x++' inputfile.txt or sed '/pattern/d' or use uniq and sort... (7 Replies)
Discussion started by: redse171
7 Replies

5. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

6. Shell Programming and Scripting

sed search pattern and delete lines

Hello, i have a question. My problem is that i have a file like: TEST JOHN ADAM MICHAEL SEBASTIAN ANDY i want find for MICHAEL and want delete lines like this: TEST (4 Replies)
Discussion started by: eightball
4 Replies

7. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

8. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

9. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

10. Shell Programming and Scripting

Multile Pattern Search in a same line and delete

HI Gurus, I need to delete a line from a syslog file, if it matches three conditions. Say for ex., if the device name is device.name.com and if it contains the syslog message PAGP-5-PORTFROMSTP in between the time period 00:00:00 to 04:00:00, then the particular line has to be deleted from... (2 Replies)
Discussion started by: sasree76
2 Replies
Login or Register to Ask a Question