I need to insert "-" minus sign in below string after 2 characters then after 3 characters like:
mystring="223334444"
and the desired output be like:
22-333-4444
I am able to insert characters at fixed length by following below question,
https://unix.stackexchange.com/questions/5980/how-do-i-insert-a-space-every-four-characters-in-a-long-line
but in my case, the splitting is not static (not after each nth positions but after x,y,z positions), I need an expression to do the job using sed.
Update
I am able to achieve the required format using multiple sed commands like :
echo 111111111 | sed 's/.\{2\}/&-/' | sed 's/.\{6\}/&-/'
but I need to know how I can I achieve it using a single expression
Adding more information// sorry that I missed it earlier
the output 22-333-444 will be used in some searching (using grep) and I will potentially have to do thousands of these conversions and then do the searching on the output strings, so here processing speed/optimization would also be desirable.
Adding information about searching part as requested in comments
inputfile sample:
135311046
135310897
135311354
135310944
125312732
125222083
415211804
415222255
415204163
415206020
I am reading this file line by line in a while loop, in a variable line
what I want to do is to convert this string 135311046 to 13-531-1046 and then do grep on a file.
I am currently using below substitution
grep $(echo $line | sed 's/.\{2\}/&-/' | sed 's/.\{6\}/&-/') datafile.txt
datafile contains data like this:
Line1.P2.ON28.C1.P1.FL1,12-522-2083
Line1.P1.ON19.C1.P1.FL1,12-522-2112
Line1.P1.ON34.C1.P1.FL1,12-530-2766
Line1.P2.ON15.C1.P1.FL1,12-531-1041
Line1.P2.ON15.C1.P3.FL1,12-531-1041
Line1.P2.ON15.C1.P4.FL1,12-531-1041
Line1.P1.ON39.C1.P1.FL1,12-531-1094
Line1.P2.ON26.C1.P1.FL1,12-531-2732
Line1.P1.ON57.C1.P1.FL1,12-533-4019
so my main requirement is to do the conversion in a nice/compact manner and since these are thousands of lines I need to search in another file, it needs to be fast/optimized
EDIT: Since OP has edited post with complete requirement so adding solution as per it now.
awk '
FNR==NR{
a[substr($0,1,2)"-"substr($0,3,3)"-"substr($0,6)]
next
}
($NF in a)' Input_file1 FS="," Input_file2
It should work in any awk IMHO. Output will be as follows.
Line1.P2.ON28.C1.P1.FL1,12-522-2083
Line1.P2.ON26.C1.P1.FL1,12-531-2732
1st Solution: Could you please try following. Here I am placing - after first 2 characters then after 3 characters. Here I am using sed's capability to store the values into memory by using \(..\) which means letting sed know to keep 1st 2 chars into memory(which later could be accessed by using \1) similarly we can create more memory place holders and can access them with their numbers later while substitution part.
mystring="223334444"
echo "$mystring" | sed 's/\(..\)\(...\)\(....\)/\1-\2-\3/'
2nd solution: Or if you want to substitute all 2's and all 3's in a string's value(this will not depend upon place of 2s and 3s it will simply substitute them in everywhere in string) then try following.
echo "$mystring" | sed -E 's/2+|3+/&-/g'
Output will be as follows.
22-333-4444
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With