0

I have this:

Issue #12345: some more text here https://some.domain/some/path

I would like to find the 12345 bit (which is dynamic - so will need to be via a regex), and then add it to the end of the same line. Like so:

Issue #12345: some more text here https://some.domain/some/path/12345

How can I achieve that using sed/awk?

PS: I have looked around, but the only similar question was this: Replacing part of a string with another part of that same string ...but it is missing the regex bit.

0

Assuming your input is in a file test.txt, the following command should work

sed -E 's/^(.*)([[:digit:]]{5})(.*)$/\1\2\3\/\2/g' test.txt

If you are not reading directly from a file,

input_source | sed -E 's/^(.*)([[:digit:]]{5})(.*)$/\1\2\3\/\2/g'

Output:

Issue #12345: some more text here https://some.domain/some/path/12345

What the command does:

^(.*) Start at the beginning of the file and grab everything until the next match ([[:digit:]]{5}) match the next 5 digits (.*)$ grab everything until the end of the file \1\2\3\/\2 each matched group is numbered (1-3 in this case) and we format the output to get the original text (matches 1-3), '/' and then the second match.

For future reference, it would be ideal if you could describe your problem more concretely. For example say that you are looking for the first 5 digits in a line and want to add these digits (with a preceding slash) to the end of the line, and have this done for every line in the input. I assumed this is what you meant. If not, you may want to update your question to be more specific.

You may also want to list some attempts you made instead of just citing previous questions. Also helps us get a better idea of what you are trying to do.

  • If your pattern is anchored, you can't expect to do more than one substitution. The /g flag is therefore not needed. – Kusalananda Jun 2 at 6:58
  • Thank you both @Kusalananda and @TAAPSogeking ...you both helped me understand the bit that I could not, so I was able to tweak things for my specific use case. I have chosen @TAAPSogeking's answer, because he better explains that anything in parentheses can be a "match" within the string, and that these can then be mapped to the \1\2\3 bit. ...I have tried to mark @Kusalananda's answer as helpful but I don't have enough reputation points for it to count. – Gregory Netsas Jun 2 at 10:22
0
sed 's,\([[:digit:]][[:digit:]]*\).*,&/\1,' file

or, if your sed has -E to deal with extended regular expressions in patterns,

sed -E 's,([[:digit:]]+).*,&/\1,' file

The sed substitution expression finds the first positive integer (string of digits) on the line and captures it. It also matches the rest of the line from that point on to the end of the line. The replacement part of the expression replaces the matched bit of the line with everything that was matched (&) followed by a slash and the captured string of digits.

I'm using commas as the delimiter in the expression since the replacement part contains a slash, but I could also have written the command as

sed -E 's/([[:digit:]]+).*/&\/\1/' file

The commands above would perform the substitution on all lines of input. To restrict it to lines that only begins with the string Issue #, use

sed -E '/^Issue #/s,([[:digit:]]+).*,&/\1,' file
0

I have done by below method

command

i=`awk '{print $2}' file.txt| sed "s/^#//g"| sed "s/:$//g"`
awk -v i="$i" '{print $0"/"i}' filetxt

output

Issue #12345: some more text here https://some.domain/some/path/12345

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.