Need some sed help

der.hans PLUGd at LuftHans.com
Tue Jul 2 07:08:51 MST 2013


Am 02. Jul, 2013 schwätzte Dazed_75 so:

moin moin,

> I have some C code with lines that look like:
> stuff1; stuff2; spPtr+=stuff3; maybesomestuff4
>
> I can find only the lines of interest using grep spPtr+=foo.c but I
> wondered if I could easily use sed to convert those lines to
> stuff1; spPtr+=stuff3; stuff2; maybesomestuff4
>
> so basically with ; as a delimiter, swapping group 2 and 3 but only for
> those lines containing "spPtr+="
>
> Any ideas?

$ echo "1; 2; 3; 4;" | sed -re 's/^([^;]+;)([^;]+;)([^;]+;)/\1\3\2/'
1; 3; 2; 4;
$

Combining the 3 capture groups into one is left as an exercise for the
reader :).

Obviously, if any of the fields contain a ;, then that match won't do what
you're wanting on that line.

ciao,

der.hans
-- 
#  http://www.LuftHans.com/        http://www.LuftHans.com/Classes/
#  Wer bereit ist, grundlegende Freiheiten aufzugeben, um sich
#  kurzfristige Sicherheit zu verschaffen, der hat weder Freiheit
#  noch Sicherheit verdient.  --  Benjamin Franklin  (1706 - 1790)


More information about the PLUG-discuss mailing list