LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices


Reply
  Search this Thread
Old 04-06-2007, 02:23 PM   #1
acascianelli
Member
 
Registered: Oct 2002
Location: Michigan
Posts: 71

Rep: Reputation: 15
Need command to search and replace text in file


I have an output file from a script command that has the string '^[[m' and '^[[1m' that need to be removed. I can not seem to get the sed command to work with those strings. Does anybody else know of a command that can parse those strings out of the file?

AIX version is 5.2
 
Old 04-06-2007, 03:37 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Are you sure it is characters and not simply the way you're seeing a control string. (^M for example is how you would see "carriage returns" in a DOS ascii file). The ^M in that case is actually a single character rather than the two characters ^ and M. You can see that by going to the character and trying to move right - if it jumps past the rest then it is a control character but if it actually goes to the M then you know it is two characters.

Assuming it IS characters you might try escaping the special ones in sed:

\^\[\[m

That way sed knows to treat them as literal characters rather than interpreting special meaning for them (e.g. ^ can mean "beginning of the line" in regular expressions [regex].)
 
Old 04-09-2007, 07:39 AM   #3
acascianelli
Member
 
Registered: Oct 2002
Location: Michigan
Posts: 71

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlightner
Are you sure it is characters and not simply the way you're seeing a control string. (^M for example is how you would see "carriage returns" in a DOS ascii file). The ^M in that case is actually a single character rather than the two characters ^ and M. You can see that by going to the character and trying to move right - if it jumps past the rest then it is a control character but if it actually goes to the M then you know it is two characters.

Assuming it IS characters you might try escaping the special ones in sed:

\^\[\[m

That way sed knows to treat them as literal characters rather than interpreting special meaning for them (e.g. ^ can mean "beginning of the line" in regular expressions [regex].)
I tried both of these commands...

sed 's/\^\[\[m//g' session.200704061648.log > new.log

...doesn't work. But the command did execute, where as before would give me an error. Any more advice? When I open the file in vi, the characters do not seem to be control characters, I can see other '^M' and those instances behave like you predicted. But, when I use the sed command with an output redirect to a file, it shows the contents of the file without the '^[[m' string.

One other thing I just noticed. When I use a 'more' command on the file, I can see the '^[[m', but when I use 'cat', they're not there.

Last edited by acascianelli; 04-09-2007 at 08:16 AM.
 
Old 04-09-2007, 08:15 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
One of the fun things when dealing with REGEX is you sometimes have to escape things AND quote your escapes. (This is because the shell is interpreting things THEN sed is. Quoting makes the shell pass through the escapes.)

Just did a test here and this worked for me:

sed -e s/'\^''\[''\['m//

Essentailly it is escaping with "\" (e.g. \^) but putting single quotes around the escape it is doing (e.g. '\^'). Since you have to escape each character but have to quote it.

It also worked just to quote the whole thing rather than the individual components:

sed -e s/'\^\[\['m//
 
Old 04-09-2007, 11:06 AM   #5
acascianelli
Member
 
Registered: Oct 2002
Location: Michigan
Posts: 71

Original Poster
Rep: Reputation: 15
Ok, no matter what I do, those characters still remain visible with a more command and vi but not with cat. I'm just going to leave it as it is, and assume that they are just control characters and that they won't be visible with a normal text editor. And even if they are.

Thanks for your help, you've cleared up alot of my questions with sed and special characters in general.
 
Old 04-09-2007, 12:22 PM   #6
pbaldera
LQ Newbie
 
Registered: Nov 2005
Location: New York City
Distribution: RHEL3,4,5
Posts: 29

Rep: Reputation: 15
These characters are mostly use to edit appearance of your output. the ^[ character is created by hitting CTRL+v, then Esc key. If you do echo "^[[31mHello^[[0m" it will print "Hello" in read if your terminal is color capable. There is no way to display these characters with a shell command since they mean something to the shell. If you want to get rid of them you have to do it from the vi session. by running your sed command from :%s/\^\[\[m//g
 
Old 04-10-2007, 07:49 AM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
So my original post was ignored apparently. I pointed out that often these characters aren't literal but are rather control strings. I'd asked the OP to check for this before trying anything.
 
Old 04-10-2007, 07:58 AM   #8
acascianelli
Member
 
Registered: Oct 2002
Location: Michigan
Posts: 71

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlightner
So my original post was ignored apparently. I pointed out that often these characters aren't literal but are rather control strings. I'd asked the OP to check for this before trying anything.
No, I didn't ignore it. They did not behave like the other control characters in the file so I assumed they were literal. I'm still getting the hang of AIX, I'm a Linux guy and I had never seen anything like that in any file under Linux before.
 
Old 04-10-2007, 09:14 AM   #9
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
OK my apologies.

One way that might work:

cat -v file >file2

This converts the non-printing characters to literal characters (usually) and you may then be able to strip the characters out with the sed syntax I gave because file2 now has it as literal characters.

I just tested the above using ^B (ctrl-B). In the original file that shows as 1 character. In the output it shows as two (^ and B).

You could probably even just pipe the "cat -v file" into the sed and output that to your result file but I haven't tried that.

Last edited by MensaWater; 04-10-2007 at 10:39 AM.
 
Old 04-11-2007, 08:38 AM   #10
acascianelli
Member
 
Registered: Oct 2002
Location: Michigan
Posts: 71

Original Poster
Rep: Reputation: 15
Yup, that works. Now that they are converted to literals they can be removed with a sed command. I'm going to mess around with it alittle more today, since the files would be viewed from a Windows system I may not need to remove the control characters. But if I do, I know how to do it now.

Thanks alot for the help.
 
Old 04-11-2007, 11:07 AM   #11
acascianelli
Member
 
Registered: Oct 2002
Location: Michigan
Posts: 71

Original Poster
Rep: Reputation: 15
One more question. I have it working now, but I need to use the 'sed' command 3 times to remove 3 string. The string I need to remove are '^[[m', '^[[1m', and '^M'. What would the sed command be to go through the entire file and remove all instances of those 3?

Last edited by acascianelli; 04-11-2007 at 11:09 AM.
 
Old 04-11-2007, 01:24 PM   #12
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You can specify "-e" with the s/pattern/replacement/" multiple times - just do it for each pattern. Also put g after the / so it does it "globally" (each time it appears in the line) otherwise it would only do the first time it appeared in the line.

e.g:
sed -e s/'\^\[\['m//g -e s/'\^\[\[1m'//g -e -s/'\^M'/g

By the way ^M is carriage return. If you run into files that have only those in them you can convert it with dos2unix command (or add it by using the unix2dos command). This a common thing when copying text files from Linux to DOS (Windows) or vice-versa.
 
Old 04-11-2007, 08:16 PM   #13
fjd
Member
 
Registered: Apr 2006
Distribution: Ubuntu 10.10
Posts: 39

Rep: Reputation: 15
A good reference for regular expressions is:
http://www.regular-expressions.info/

FJD
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 03:07 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

LinuxQuestions.org > Forums > Other *NIX Forums > AIX

All times are GMT -5. The time now is 03:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration