Reputation:
i have problem with substitution. I have a file with 1 line of random characters with brackets "{}" around 1 character. I want to move with these brackets on the previous character or on the next one. (I know how to do it if the line of character is still, non-changing.) But I wonder how to do it when I don't know these chars and I don't know where these brackets are.
For Example: " ABC123{X}CBA321 " ==> " ABC12{3}XCBA321 " or " ABC123X{C}BA321 "
I would like to use awk or sed, some regex, maybe...
Upvotes: 1
Views: 249
Reputation: 959
I tried in perl. this will move the bracket to the previous character
my $a="ABC123{X}CBA321";
if ($a =~ /(.)\{(.)\}/)
{
print "$`"."{$1}";
print "$2";
print "$'";
}
NOTE: $` is string before the match & $' is string after match
Upvotes: 0
Reputation: 90012
Move backward one character:
sed -e 's/\(.\){\(.\)}/{\1}\2/g' file
Move forward one character:
sed -e 's/{\(.\)}\(.\)/\1{\2}/g' file
To modify the file in-place, use the -i
flag:
sed -i -e 's/\(.\){\(.\)}/{\1}\2/g' file
sed -i -e 's/{\(.\)}\(.\)/\1{\2}/g' file
The first example works by matching any character followed by a character surrounded by {}
. Without grouping, this is: .{.}
We add grouping so we can put the two characters in the output. Instead of surrounding the second character with {}
with surround the first character. This is {\1}\2
.
The second example works similarly, but matches {.}.
first then outputs \1{\2}
.
Upvotes: 5
Reputation: 4283
Here's a small example.
$ echo "ABC123{X}CBA321" | sed -e 's/\(.\){\(.\)}\(.\)/{\1}\2\3/'
ABC12{3}XCBA321
$ echo "ABC123{X}CBA321" | sed -e 's/\(.\){\(.\)}\(.\)/\1\2{\3}/'
ABC123X{C}BA321
Here's how to edit the file in place with sed.
$ sed -i -e 's/\(.\){\(.\)}\(.\)/{\1}\2\3/' file
$ sed -i -e 's/\(.\){\(.\)}\(.\)/\1\2{\3}/' file
Upvotes: 1
Reputation: 19137
This will move the brackets to the previous character:
sed -e 's/\(.\){\(.\)}/{\1}\2/g' < in_file > out_file
This will move the brackets to the next character:
sed -e 's/{\(.\)}\(.\)/\1{\2}/g' < in_file > out_file
Upvotes: 3