regex


Replace string with another string based on backreference in bash


I'm trying to convert a predefined string %c# where # can be some number with another string. The catch is that the length of the other string must be truncated to # number of characters.
Ideally these set of commands would work:
FORMAT="%c10"
LAST_COMMIT="5189e42b14797b1e36ffb7fc5657c7eea08f1c0f"
echo $FORMAT | sed "s/%c\([0-9]\+\)/${LAST_COMMIT:0:\1}/g"
but clearly there is a syntax error on the \1. You can replace it with a number to see what I'm trying to get as output.
I'm open to using some other program other than sed to achieve this but ideally it should be programs that are pretty much native to most linux installations.
Thanks!
This is my idea.
echo ${LAST_COMMIT} | head -c $(echo ${FORMAT} | sed -e 's/%c//')
Get number with sed and get first some character with head.
EDIT1
This might be better.
echo ${LAST_COMMIT} | head -c $(echo ${FORMAT} | sed -e 's/%c\([0-9]\+\)/\1/')
EDIT2
I make the script because it is too tough to understand. Please try this.
$ cat sample.sh
#!/bin/bash
FORMAT="%b-%t-%c10-%c5"
LAST_COMMIT="5189e42b14797b1e36ffb7fc5657c7eea08f1c0f"
## List numbers
lengths=$(echo ${FORMAT} | sed -e "s/%[^c]//g" -e "s/-//g" -e "s/%c/ /g")
## Substitute %cXX to first XX characters of LAST_COMMIT
for n in ${lengths}
do
to_str=$(echo ${LAST_COMMIT:0:${n}})
FORMAT=$(echo ${FORMAT} | sed "s/%c${length}/${to_str}/")
done
## Print result
echo ${FORMAT}
This is the result.
$ ./sample.sh
%b-%t-5189e42b1410-5189e5
Also this is one line commands (Same contents but too long and too tough)
for n in $(echo ${FORMAT} | sed -e "s/%[^c]//g" -e "s/-//g" -e "s/%c/ /g"); do to_str=$(echo ${LAST_COMMIT:0:${n}}); FORMAT=$(echo ${FORMAT} | sed "s/%c${length}/${to_str}/"); done; echo ${FORMAT}
The value of $LAST_COMMIT gets interpolated before sed runs, so there is no backreference to refer back to yet. There is an /e extension in GNU sed which would support something like this, but I would simply use a slightly more capable tool.
perl -e '$fmt = shift; $fmt=~ s/%c(\d+)/%.$1s/g; printf("$fmt\n", #ARGV)' '%c10' "$LAST_COMMIT"
Of course, if you can let go of your own ad-hoc format string specifier, and switch to a printf-compatible format string altogether, just use the printf shell command straight off.
length=$(echo $FORMAT | sed "s/%c\([0-9]\+\)/\1/g")
echo "${LAST_COMMIT:0:$length}"

Related Links

Regex to determine if string contains all the vowels? [closed]
Regex matching “a > b > c” pattern?
Notepad++ Regex replace complete string within double quotes. Tried all possibilities with no use
What is the meaning of the following awk script example?
Regular expression optional match start/end of line
preg_replace with UTF-8 letter
regex just have 2 items [closed]
Regular expression find and replace Notepad ++
regex wont find a match
Selective regex on a word/character
Why doesnt [\n$] work while (\n|$) does?
How to create an array of regex matches
Greedy and non-greedy regex
How can find and replace muiti text at once?
regex for float with 6 decimals and 3 numbers
regex invalid expression nothing to repeat

Categories

HOME
ios
common-lisp
oozie
web-services
auth0
order
swashbuckle
google-cloud-dataproc
vsix
gluon
asciidoc
librsvg
command-line-interface
vulkan
w2ui
durandal-2.0
jruby
sencha-touch-2
robotium
liferay-aui
php-carbon
vnc
code-signing
toolbar
meanjs
apscheduler
capstone
powerbuilder-conversion
texture-mapping
sourcetree
runc
mindstorms
eve
wso2iot
karnaugh-map
virtualmin
http-status-code-500
domdocument
glfw
harvest-scm
jawr
easyhook
gpt
replicate
vistadb
tachyon
vispy
cakephp-2.3
logstash-jdbc
android-chips
kernighan-and-ritchie
selenium2library
webmail
okuma
genetic
quasar
oboe.js
telescope
mailx
bloodhound
stringr
odoo
words
puppet-enterprise
mpj-express
controlsfx
ash
aps
grunt-contrib-copy
batman.js
appstore-sandbox
mod-python
bochs
adlds
godaddy-api
dependency-walker
java-ee-5
jmdns
ekeventkit
cellphone
ruby-1.9.2
android-3.1
symstore
program-transformation
markitup
crc-cards
nhaml
database-dump
plumtree

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App