LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2013, 02:02 AM   #1
bengerman
LQ Newbie
 
Registered: Jul 2013
Posts: 1

Rep: Reputation: Disabled
using grep and sed to find and replace a string with a pattern (HELP)


I am trying to figure out how to find and replace any hexadecimal string using wildcards. For example if I had a text file in the /etc folder with a text file containing 5g:4f:3e:42 and have another text file with 4e:5c:3d:62 and replace both of this string with 22:22:22:22 simultaneously.
Right now I have this script written out which works to find anyfile contain the word oldstring but I am not sure how to use wildcards and patterns to find my hex string.

grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'

-TheGerman
 
Old 07-02-2013, 02:30 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
For those specific strings in 2 files here's an example
Code:
 cat t1.t t2.t

5g:4f:3e:42
32:34:36:ae

4e:5c:3d:62
12:ad:34:ea

 sed -r 's/5g:4f:3e:42|4e:5c:3d:62/22:22:22:22/' t1.t t2.t

22:22:22:22
32:34:36:ae

22:22:22:22
12:ad:34:ea
HTH


To change all hex patterns with 4 elements
Code:
sed -r 's/([[:alnum:]]{2}:){3}[[:alnum:]]{2}/22:22:22:22/' t1.t t2.t
22:22:22:22
22:22:22:22

22:22:22:22
22:22:22:22
 
Old 07-02-2013, 02:32 AM   #3
Linux MR
Member
 
Registered: May 2013
Location: Pacific North West
Distribution: Red Hat, Scientific, Fedora, openSuSE, Mageia, Mint, Arch
Posts: 57

Rep: Reputation: Disabled
it's ugly (to me) but this should work to just show the replacement

egrep => use extended grep
sed -e => do operation on screen/terminal, don't change file

Quote:
egrep '??:??:??:??' <file_name> | sed -e 's/[a-zA-Z0-9][a-zA-Z0-9]:[a-zA-Z0-9][a-zA-Z0-9]:[a-zA-Z0-9][a-zA-Z0-9]:[a-zA-Z0-9][a-zA-Z0-9]/22:22:22:22/g'
Hope that gives some inspiration
#LQOSCON

Last edited by Linux MR; 07-02-2013 at 02:33 AM.
 
Old 07-02-2013, 02:38 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Are you sure your string pattern will be 4e:5c:3d:62 not the 4ej:5hc:3df:622

If there is colon after first two digits try
Quote:
grep -r "..:.*" /path/of/file
grep -r ..:..:..:.. /path/of/file
grep -rw "..:..:..:.." /root/Desktop/test/a
Quote:
grep -re "..:..:..:.. " -re "^..:..:..:..$" -re "^..:..:..:.." -re "..:..:..:..$" -re " ..:..:..:.. " -re " ..:..:..:.." /path/of/file

Last edited by eklavya; 07-02-2013 at 02:40 AM.
 
Old 07-02-2013, 03:09 AM   #5
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 502

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by bengerman View Post
I am trying to figure out how to find and replace any hexadecimal string using wildcards. For example if I had a text file in the /etc folder with a text file containing 5g:4f:3e:42 and have another text file with 4e:5c:3d:62 and replace both of this string with 22:22:22:22 simultaneously.
Right now I have this script written out which works to find anyfile contain the word oldstring but I am not sure how to use wildcards and patterns to find my hex string.

grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/'

-TheGerman
First '5g:4f:3e:42' is not HEX number. HEX range is 0-9 and A-F (total 16). the letter 'g' is out of range for HEX code. Assuming it is lesser or equal to 'f', then

Code:
$ sed -r -n s/\([0-9a-fA-F]{2}:\){3}[0-9a-fA-F]{2}/22:22:22:22/gp file
Second, if you want in-file replacement with backup file, then
Code:
$ sed -r -i.bak s/\([0-9a-fA-F]{2}:\){3}[0-9a-fA-F]{2}/22:22:22:22/g file
Note: You can also replace hexadecimal [0-9a-fA-F] with [[:xdigit:]]

Last edited by mddnix; 07-02-2013 at 03:19 AM.
 
Old 07-03-2013, 01:53 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Linux MR View Post
Code:
egrep '??:??:??:??' <file_name> | sed -e 's/[a-zA-Z0-9][a-zA-Z0-9]:[a-zA-Z0-9][a-zA-Z0-9]:[a-zA-Z0-9][a-zA-Z0-9]:[a-zA-Z0-9][a-zA-Z0-9]/22:22:22:22/g'
The output of grep will be only the matching line(s), and sed will only process those lines. You can't alter the original file this way without destroying data.

I'm not completely sure I understand what the OP wants to do here. A more detailed explanation (particularly the part about "wildcards") and clearer, longer examples would help.
 
  


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
[SOLVED] use sed to find string pattern and delete subsequent characters jigg_fly Programming 16 10-10-2013 09:34 AM
[SOLVED] XML find and replace tags with grep or sed Darkhoros Programming 2 05-30-2013 03:59 PM
[SOLVED] BASH (grep / sed / awk): find string within a (line) range dragonetti Linux - Newbie 2 11-24-2011 11:16 PM
Help to replace a pattern with sed or awk rramesh1 Linux - Newbie 2 11-22-2011 01:06 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM


All times are GMT -5. The time now is 06:56 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration