sed replace multiple occurrences on the same line, but not all


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace multiple occurrences on the same line, but not all
# 1  
Old 08-03-2009
sed replace multiple occurrences on the same line, but not all

Hi there!

I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance:

I have a command which the output will be:

Code:
200.300.400.5 0A 0B 0C 01 02 03

being that the last 6 strings are actually one MAC address

I wanted to re-write the output of the command so it will come out like this:

Code:
200.300.400.5 0A0B.0C01.0203

Considering space " " as a delimiter, it will ALWAYS join 2nd and 3rd fields, then replace " " with "." between 3rd and 4th fields, join 4th and 5th fields, replace " " with "." between rth and 6th fields.

It can be something other than sed as well.

It is a bonus if the script also transform "CAPS" into "small caps"

Code:
200.300.400.5 0a0b.0c01.0203

Cannot find how to make sed replace certain occurrences on the same line! Smilie
# 2  
Old 08-03-2009
Try awk
Code:
awk '{$0=tolower($1FS$2$3OFS$4$5OFS$6$7)}1' OFS="." file

Quote:
Originally Posted by ppucci
It is a bonus if the script also transform "CAPS" into "small caps"
Smell like a homework.
# 3  
Old 08-03-2009
ok... supose the file has multiple lines... will that work?

even with one line I am trying here and I get error:

Code:
home:~> cat 10.3.2.10_VLAN36_MACS
248.88.41.141 00 0F F8 58 29 8D
home:~> awk '{$0=tolower($1FS$2$3OFS$4$5OFS$6$7)}1' OFS="." 10.10.10.10_VLAN36_MACS
awk: syntax error near line 1
awk: bailing out near line 1
home:~>

I am using Solaris 5.10 by the way

Last edited by ppucci; 08-03-2009 at 04:59 PM..
# 4  
Old 08-03-2009
Code:
# cat 10.3.2.10_VLAN36_MACS
248.88.41.141 00 0F F8 58 29 8D

# awk '{$0=tolower($1FS$2$3OFS$4$5OFS$6$7)}1' OFS="." 10.3.2.10_VLAN36_MACS
248.88.41.141 000f.f858.298d

Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk) and pay attention to file names :!
# 5  
Old 08-03-2009
got nawk, works like a charm! thank you! Smilie

by the way... not homework... trying to automate some work stuff Smilie

Last edited by ppucci; 08-03-2009 at 05:11 PM..
# 6  
Old 08-03-2009
Code:
$_="200.300.400.5 0A 0B 0C 01 02 03";
s/ (?=[^ ]+( [^ ]+ [^ ]+)*$)//g;
s/([A-Z]+)/lc($1)/eg;
my @tmp=split(" ",$_,2);
$tmp[1]=~s/ /./g;
print $tmp[0]," ",$tmp[1];

# 7  
Old 08-04-2009
Sed

HOW can we swap two fields in every line of text using SED command
can this command works?
sed s/$1/$2/g file[/I][/B]

---------- Post updated at 12:24 PM ---------- Previous update was at 12:19 PM ----------

[quote=krkrishna.cse;302340582]HOW can we swap two fields in every line of text using SED command
can this command works?
sed s/$1/$2/g file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete multiple occurrences of the same pattern on a line but the first

The lines that I am trying to format look like Device ID: j01-01, IP address: 10.10.10.36, IP address: 10.10.10.35, IP address: 10.10.102.201, Platform: 8040, Capabilities: Host , Interface: GigabitEthernet9/45, Port ID (outgoing port): e0k,Here is what I have so far but it... (4 Replies)
Discussion started by: dis0wned
4 Replies

2. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

3. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

4. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

6. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

7. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

8. Shell Programming and Scripting

SED : Replace whole line on multiple execution

Hi, I am have one file with a line group=project_live I need to replace it with line group=project_live_support before I execute some application related script. The potentianl problem is when I replace this with sed using command sed... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

9. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

10. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies
Login or Register to Ask a Question