Sed Insert symbol before in position - linux

I have one problem with my sed command. Got string foooooooBaaaar and I need comma separate before Baaaar to be fooooooo,Baaaar. Maybe whom knows how can i do it and can explain me ? Thanks!

To insert a comma before an uppercase letter:
$ sed 's/[A-Z]/,&/' <<< "foooooooBaaaar"
fooooooo,Baaaar
Explanation:
s: substitute
[A-Z]: uppercase letter
with comma followed by the matching letter(&)
To add a comma before Baaaar string:
$ sed 's/Baaaar/,&/' <<< "foooooooBaaaar"

Related

Deleting characters from permutations and character combinations

To delete particular characters from a combination list.
printf "%s\n" {a..c}{a..d} | sed 's/^cc//' | tr -s '\n'
I used the code above to delete a particular line of character from combination. Is there a way I can do it without sed, awk, grep or bc. Can I get it done with a single line of code in the script?
If you have stored your values in an array, e.g.:
arr=({a..c}{a..d})
Then you may filter your array elements with a string substitution:
printf -- '%s\n' "${arr[#]/%cc/}"
The syntax ${arr[#]/%cc/} tells to parse all elements from the array arr and substitute %cc with nothing. The % character indicates the beginning of the string, similar to ^ in sed, thus %cc means "every string beginning with cc".

How to replace a string between two commas in linux using sed

execute PKG_SP_MAINTENANCE.MoveAccount(91, 129031, 958408630); Lowes
From the above statement I am trying to get the content between the first comma and second comma i.e., 129031 and replace it with a new string which is passed as a parameter to the script. For now let's replace with N . I tried the following sed command ended up getting an error. Could someone please help?
04:24:01 Tue Sep 19 [serviceb#LQASRDSBMST002V:~/isops/tmp] cat Navya | sed 's/,^.\{*\},/N/'
sed: -e expression #1, char 14: Invalid content of \{\}
$: echo "start,middle,end" | sed 's/,[^,]*,/,NEW,/g'
start,NEW,end
Is this what you mean? This simply matches the inner-most commas and replaces the text.
Depending how you want to handle strings with more than two commas, you could do something like this to match the outer-most instead:
$: echo "start,middle,end" | sed 's/,.*,/,NEW,/g'
start,NEW,end

Replacing a string with empty line

I have a file which has 2000 lines of data (file name is data.tsv). I want to replace the string with empty line where there is a matching pattern, in my case is PMC:
How can I do with Vim or other sed command?
Thanks,
Rio
With vim you can do it like this:
:g/PMC:/normal S
Try this with GNU sed:
sed -i 's/.*PMC:.*//' data.tsv
Another way is:
:%s/.*PMC:.*//
where the '%' means 'every line' and the 's' is subsitute. An alternate:
:g/PMC:/s/.*//
where the 'g/PMC:/' is short for 'global if line contains "PMC:"' and the 's/.*//' means 'match anything and replace it with the empty string'

Using sed to match anything and \s

I've got the following:
sed -i "s/SYNFLOOD_RATE = \"100/s\"/SYNFLOOD_RATE = \"10\s\"/g"
Question is how do I avoid this error?
/bin/sed: -e expression #1, char 28: unknown option to `s'
And is there a way to do a wild card match and replace with sed?
You have too many slashes, 4 when there should be 3. Use a different delimiter; comma (,), bang (!), hash (#), and at (#) are common alternatives.
sed -i "s,SYNFLOOD_RATE = \"100/s\",SYNFLOOD_RATE = \"10\s\",g"
Note that you have "100/s" in the original and "10s" (no slash) in the replacement. To actually insert a backslash, you'd need to enter 4 of them: 10\\\\s. Each pair will get reduced to a single by the shell and then the remaining double will be interpreted as a literal backslash by sed.
If you want to first grep then substitute :
sed -i '/SYNFLOOD_RATE = \"100/s/"\/SYNFLOOD_RATE = \"10\s\"/replacement/g'
But the delimiter can be anything else than /, see :
sed -i '/SYNFLOOD_RATE = "100/s#"/SYNFLOOD_RATE = "10\s"#replacement#g'
( the delimiter here is #)

how to replace substring in a file according to specific pattern without programming

suppose I have a file:
its format should be :
number, string1 , [string2] ,....
here string1 should not contain ',' ,because we use ',' to separate each column
but due to some reason ,string1 now contain some ',' inside it,
so we need to replace it with other symbol ,such as '-'
1,aaa,bbb,ccc,[x,y,z],eee,fff,ggg
2,q,w,[x],f,g
3,z,[y],g,h
4,zzz,xxx,ccc,vvv,[z],g,h
....
should be revised to :
1,aaa-bbb-ccc,[x,y,z],eee,fff,ggg
2,q-w,[x],f,g
3,z,[y],g,h
4,zzz-xxx-ccc-vvv,[z],g,h
....
what's the best way to do it without programming , I mean we just use awk,sed,vim rather than shell programming,python,c++,etc
Thanks
$ awk -F, 'BEGIN{OFS=FS} {two=$0;sub($1 FS,"",two);sub(/,[[].*/,"",two);gsub(/,/,"-",two); rest=$0;sub(/^[^[]*/,"",rest); print $1,two,rest}' input.txt
1,aaa-bbb-ccc,[x,y,z],eee,fff,ggg
2,q-w,[x],f,g
3,z,[y],g,h
4,zzz-xxx-ccc-vvv,[z],g,h
$
Let's break out the awk script for easier commenting.
$ awk -F, '
BEGIN { OFS=FS }
{
two=$0; # Second field is based on the line...
sub($1 FS,"",two); # Remove the first field,
sub(/,[[].*/,"",two); # Remove everything from the [ onwards,
gsub(/,/,"-",two); # Replace commas in whatever remains.
rest=$0; # Last part of the line, after "two"
sub(/^[^[]*/,"",rest); # Strip everything up to the [
print $1,two,rest; # Print it.
}
' input.txt
a little long, but you can use sed like this:
sed ':loop; s/\([0-9]\+,.*\)\([^,]*\),\([^,]*\)\(.*,\[\)/\1\2-\3\4/; t loop' \
input_file
slightly shorter one:
sed ':loop; s/\([0-9]*,[^\[,]*\),\([^\[,]*,\[\)/\1-\2/; t loop' input_file
description for the second one:
loop while there are matches # :loop;
1) find numbers followed by a comma, # \([0-9]*,
followed by anything not comma or '[', # [^\[,]*\)
2) find comma # ,
3) find anything not ',' or '[' # \([^\[,]*
4) followed by a ',' and '[' # ,\[\)/
5) replace the whole thing with
match of step 1 and '-' and matches
from steps 3-4 # /\1-\2/;
end loop
# t loop
This might work for you (GNU sed):
sed -e 's/,\[/\n&/;h;s/\n.*//;s/,/-/2g;G;s/\n.*\n//' file

Resources