How to copy string before comma in file and paste it later in file after slash - linux

How can I use sed
to copy before comma and paste after slash in a file?
Example:
text1,Information /
text2,Information /
text3,Information /
and I want
text1,Information /text1
text2,Information /text2
text3,Information /text3

This works with your example:
sed -r 's#(.*),(.*)/#\1,\2/\1#' <file_path>
The idea is that groups (between parentheses) are referenced in the substitution part.

This might work for you (GNU sed):
sed -E 's/([^,]*).*/&\1/' file
Capture everything before the , and append to the end of the line.

Related

SED : replace part of syntax to lower case

Original Text file:
.ABC0 (ABC0),
.EFG2 (EFG2),
.ZZZ3 (ZZZ3),
How to convert this part to
.ABC0 (abc0),
.EFG2 (efg2),
.ZZZ3 (zzz3),
with SED command easily?
There's issue to make it work.
echo ".ABC(ABC)," | sed -e 's/\(.*\.[A-Z]*\(\)\([A-Z]*\)\)/\1\L\2\E/ p'
You were almost there.
sed -e 's/\(\.[A-Z0-9]*\)\( ([A-Z0-9]*)\)/\1\L\2\E/g'
Remove the .* from the beginning, it matches as much as it can, i.e. it skips to the last occurrence of the pattern.
Include the digits in the character classes.
Use ( without a backslash to match a parenthesis literally.
This might work for you (GNU sed):
sed 's/([^)]*)/\L&/g' file
Replace the contents of parenthesis by its lowercase equivalent.

How to find and replace a file path (along with special characters) using SED command

How to find and replace a file path like a/b/c/d into x/y/z using sed command?
I tried sed -i -e 's/a/\b/\c/\d/x/\y/\z' file.txt but it did not work.
Just use another separator for s command. You can use any character. Just / is the most popular one. I like # or #. Then you have to escape that character.
sed 's#a/b/c/d#x/y/z#'
but it did not work
To escape / use \/ not /\. The \ needs to be in front of /. You could:
sed 's/a\/b\/c\/d/x\/y\/z/'

Find and replace string with "/" recursively in a directories in RHEL 7.4

I have to find and replace all the occurrence of a string in all files /eOffice/eofficev6 to /eOffice/SAPS/eofficev6 recursively in a directory in RHEL 7.4.
Problem is that I using sed -i but my string also containing / slash.
How to replace all string having /?
You have two ways to achieve what you want.
One: Escape slashes (E.G. sed -i "s/\/eOffice\/eofficev6/\/eOffice\/SAPS\/eofficev6/" file).
Two: change the delimiter (E.G. sed -i "s|/eOffice/eofficev6|/eOffice/SAPS/eofficev6|" file).
You could do
sed -i 's/\(\/eOffice\)\(\/eofficev6\)/\1\/SAPS\2/' input_file_name
The "/eOffice" and "eofficev6" parts are grouped and the "SAPS" is inserted in between.
For example, if the input is:
/eOffice/eofficev6
the output will be
/eOffice/SAPS/eofficev6
Forward slashes are escaped with \s.
Or without grouping just,
sed -i 's/\/eOffice\/eOfficev6/\/eOffice\/SAPS\/eOfficeb6/' input_file

Add many quotation in sed instruction

I have to update line with value containg many quotation characters
sed -i 's/.*sonar.links.issue.*/property "sonar.links.issue", "http://jra.url"'
I get error there. What should i do to do it correctly?
Your replacement string contains slashes "http://", so you shouldn't use / as the delimiter for this sed command, use # as shellter proposes.
To complete its answer, I would add the "g" flag, at the end of the sed expression, to ensure the subsitution is made on the whole line, and not only on the first match with ".sonar.links.issue.".
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#g' file
Try
sed -i 's#.*sonar.links.issue.*#property "sonar.links.issue", "http://jra.url"#' file
IHTH

delete and replace a line using linux command

I am trying to delete a line with the pattern matches and replacing the entire line with the another line using sed command.
File contents:Sample.txt
Testfile=xxxx
Testfile3=uuuu
Testfile4=oooo
Testfile5=iiii
Testfile2=ikeii
I am using sed command to delete a line contains Testfile3=* and replace by Testfile3=linechanged
sed -i 's/Testfile3=\*/Testfile3=linechanged/' Sample.txt.
But it just appends the replaceable string in the line as shown below
Testfile3=linechanged=uuuu.
I am expecting the output to be
Testfile3=linechanged.
What i am doing wrong?
The star is not matched right:
sed -i 's/Testfile3=.*/Testfile3=linechanged/' Sample.txt
# ^^
.* matches any character (.) for any length (*), so it will match everything till the end of the line.
You can use captured group to keep what will be preserved and use the desired replacement for the rest:
sed -i 's/^\(Testfile3=\).*/\1linechanged/' file.txt
In your case, escaping the Regex token * like \* will match * literally e.g. Testfile3=* would be matched then.
This might work for you (GNU sed):
sed '/Testfile3/cTestfile3=linechanged' file
This matches the line containing Testfile3 and changes it to the required string.

Resources