Searching and replacing '\M' in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching and replacing '\M' in file
# 1  
Old 04-27-2009
Searching and replacing '\M' in file

Hi All,

I am new to shell scripting, please help!

On regular basic we get file in different directory which contains either \M in the file at the starting of the line or at the end. I have to replace the same with blank.
I have tried writing the script however it's not working, can some one please help.

Quote:
du -k |awk '{print $2}'>file0
for i in `cat file0`
do
if [ -f $i ]
then
nawk -s '/\M\ \' $i | `mv $i $i.ch`
fi
done
Thanks
# 2  
Old 04-27-2009
is it the content of the File will have \M or the file name ?..

In the First case :

Ex : cat file.txt

end \M
sample text \M bet ween
\M Start

TEST>sed -e 's/^\\M//g' -e 's/\\M$//g' file
end
sample text \M bet ween
Start

Will remove \M only if is at the begining or end of the line.
# 3  
Old 04-27-2009
Thanks

\M is in the content of file
# 4  
Old 04-27-2009
check this out for searching \M at the starting of all statement and replace it with blank space.
cat ref | sed 's/^\\M/ /'

check this out for searching \M at the end of all statement and replace it with blank space.

cat ref | sed 's/\\M$/ /'


Input file is "ref" and contents of it as below
\MMdelhi
new\M
unix\Mtechnologies\M
tech\Mdiscuss\M

OUTPUT as replace \M at starting is as below
Mdelhi
new\M
unix\Mtechnologies\M
tech\Mdiscuss\M


OUTPUT to replace at end of statement is as below
\MMdelhi
new
unix\Mtechnologies
tech\Mdiscuss
# 5  
Old 04-27-2009
Thanks Pradeep !!

How about “ du -k |awk '{print $2}'>file0 “? Is this correct as I am trying to find the files which are at the given file system, mean if there is four different directory having different file which content \M . eg
/app/log/dir1/file1
/app/log/dir2/file1
/app/log/dir3/file2
/app/log/dir4/file1

i am taking the last columns of du –k and writing to file file0 which I am passing for \m Check.

Also, can you guys direct me towards some good sed tutorials ?
# 6  
Old 04-27-2009
hello kumarmani,
You can check out this link
Sed - An Introduction and Tutorial

If you want to go through other tutorials . You can search for "sed tutorial" in google.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching file for pattern, output to file (BASH)

Hello, I'm trying to write a script in Bash to assist in pentesting. Essentially I'm looking to use a script to search for some terms in a log file and then send that key information into another file. The log files consist of HTTP and SSL information that someone creates while browsing and... (2 Replies)
Discussion started by: Sagesparten007
2 Replies

2. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

3. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

4. Shell Programming and Scripting

searching multiple lines and replacing in shell scripting

Hi, I have a file with below contents, ssenthil = rw anilkg = rw I want to search for "ssenthil" and need to delete line 1 and 2 , if the third line starts with "" respectively and blank line immediately and third line starts with " anilkg = rw Please help me . Great day... (5 Replies)
Discussion started by: anil8103
5 Replies

5. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

6. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

7. Shell Programming and Scripting

searching & replacing/removing only certain HTML tags

I generally save a lot of web pages for reading offline which works out great for school. Now I have to spend a lot of time on the bus and I am looking for the best way to read some of these webpages using my Nokia 7610. I have uploaded the files to my phone, but they are deadly deadly slow to... (2 Replies)
Discussion started by: naphelge
2 Replies

8. Shell Programming and Scripting

searching a date and replacing with another date

I have a text file that i want to search through and pick out any dates that are formatted like MM/DD/YYYY and replace them with a date i want like 10/29/2009. any idea show i would do this?:) Snapshot of my text file: test4>s44syd5172>070>528>ENU>nongnuan>wanrawee>sr2330532>... (7 Replies)
Discussion started by: infiant
7 Replies

9. UNIX for Dummies Questions & Answers

searching through a file and replacing

Hi can anyone show me how to search through a file (multiple columns) and based on say one column if empty and another being popluted and if both senario's are true then get information from another file matching the data from the populated column based upon the populated column and bring through... (2 Replies)
Discussion started by: Gerry405
2 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question