LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-10-2007, 02:40 AM   #1
RattleSn@ke
Member
 
Registered: Oct 2007
Location: Netherlands, ZH
Posts: 32

Rep: Reputation: 15
Question [SOLVED] Problem changing line with sed. Should i use AWK?


Hi all!

I'm struggling with a problem with sed (or awk).
I have several different files which need a change on a certain line.
The files contain lines like:
B-1234/ 0001ABCD-0023ASDF-W-5--
The next file has a line like:
B-1234/ 0001ABCD-0023EFVB-W-5--

I need to find a way to change ASDF and EFVB into ABCD.
What I don't want is to search for the strings that has to be replaced, because they change over time.
What I want is to replace the 4 characters starting of position 22 with ABCD.

I'm searching for two days now but it seems I can't get hold of the right construction.

Please help me find the right construction.

TIA!
Onno.

Last edited by RattleSn@ke; 11-11-2007 at 05:48 AM.
 
Old 11-10-2007, 05:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If I understand correctly, you only want to change positions 23 thru 26 with ABCD, no matter what the first 22 or the last 6 are. If that is the case:

sed 's/\(.\{21\}\)\(.*\)\(.\{6\}\)/\1ABCD\3/' infile

Example run:
Code:
$ cat infile 
B-1234/ 0001ABCD-0023ASDF-W-5--
B-1234/ 0001ABCD-0023EFVB-W-5--
W-4321/ 0431ABCD-0024ASDF-X-6--
W-4321/ 01A43BCD-0024EFVB-X-6--
Z-4477/ 3301ABCD-0025ASDF-Y-7--
Z-4477/ 3301ABCD-0025EFVB-Y-7--

$ sed 's/\(.\{21\}\)\(.*\)\(.\{6\}\)/\1ABCD\3/' infile 
B-1234/ 0001ABCD-0023ABCD-W-5--
B-1234/ 0001ABCD-0023ABCD-W-5--
W-4321/ 0431ABCD-0024ABCD-X-6--
W-4321/ 01A43BCD-0024ABCD-X-6--
Z-4477/ 3301ABCD-0025ABCD-Y-7--
Z-4477/ 3301ABCD-0025ABCD-Y-7--
It's up to you to figure out the sed command used If you cannot figure it out, just ask.

Hope this helps.
 
Old 11-10-2007, 07:32 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
sed 's/\(.\{21\}\)\(.*\)\(.\{6\}\)/\1ABCD\3/' infile
Thereby proving that one can write totally inscrutable commands using SED.......

3 groups in "\()\" for backreference....loosely translated as:

group1: 21 of any character
group2: any # of characters
group3: 6 of any character

Replace with: group1 + ABCD + group3

Note that this will be VERY dependent on the format of the original file.

Another method would be to replace the 2nd group of 4 capital letters:

Quote:
sed 's/[A-Z]\{4\}/ABCD/2'
Even this is dependent on the exact structure of the file entries

Last edited by pixellany; 11-10-2007 at 08:23 AM. Reason: fixed boo-boo
 
Old 11-10-2007, 08:12 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@pixellany: Shouldn't that be: Replace with: group1 + ABCD + group3

I do like the shorter example you give (keeping it 'as simple' as possible).
 
Old 11-10-2007, 08:22 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
@pixellany: Shouldn't that be: Replace with: group1 + ABCD + group3
Of course!!! (fixed in the original)
 
Old 11-11-2007, 05:47 AM   #6
RattleSn@ke
Member
 
Registered: Oct 2007
Location: Netherlands, ZH
Posts: 32

Original Poster
Rep: Reputation: 15
Many thanks guys!!

It indeed solved the 'problem'.
Now I'm changing the scripts so it will only update a certain line, because each file only contains one line like C-7906/ 0001AASU-0031DNGS-M-9-- and the rest of the file don't need any changes.
With the current command to many lines get changed.

So I'm going to use grep -i -n "<needle>" <haystack> to get the right linenumber and feed that one to the sed command!!

Thanks again!!
From a windy netherlands,
Onno.
 
Old 11-11-2007, 06:08 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You probably can do that with one sed command (no need to use grep and sed). Something like this:

sed '/^C-4321/s/\(.\{21\}\)\(.*\)\(.\{6\}\)/\1ABCD\3/' infile will only change a line if it starts with C-4321

Wildcards are also possible:

$ sed '/^[ACE]/s/\(.\{21\}\)\(.*\)\(.\{6\}\)/\1ABCD\3/' infile This will change only those lines that start with an A, C or E.

Hope this helps.
 
Old 11-12-2007, 01:03 PM   #8
RattleSn@ke
Member
 
Registered: Oct 2007
Location: Netherlands, ZH
Posts: 32

Original Poster
Rep: Reputation: 15
Thanks for the tip druuna.
But I still have to use grep since the lines start everytime with a different 'key'. I already changed my script and it's working without a hitch.

But thanks again!
Onno.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 11:06 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
Problem with awk and/or sed Fluxx Slackware 10 07-08-2007 04:25 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:34 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