remove leading characters from 12 digit numbers - sed

I have a text file with id and number. All numbers are expected to be 10 digits long. In the following example only the 3rd row is correct.
36000003326|917668001520
36000003359|919005119822
36000003417|9153914209
36000003508|919454102627
If the number is 12 digits long, the first 2 country digits should be removed. The expected results are as follows:
36000003326|7668001520
36000003359|9005119822
36000003417|9153914209
36000003508|9454102627
The following sed is working but it does not check if the target entry is 10 digits or 12 digits long.
sed 's/|91/|/'

How about this?
sed -r 's/\|..(.{10})/|\1/'

One way using awk:
awk -F"|" 'length($2)>10{$2=substr($2,3);}1' OFS="|" file

This migh work for you (GNU sed):
sed -r 's/[0-9]{2}([0-9]{10})$/\1/' file

Related

sed pattern to insert a character after first m characters and then after every n characters

If I have a string and I want to use sed such that a colon is inserted after the first 8 characters and then after every 2 characters after it, what would the sed pattern look like and what would the replacement pattern look like?
awk has substr() function, it can solve your problem eaiser:
awk -v m="8" -v n="2"
'{a=substr($0,1,m);
b=substr($0,m+1);
gsub(".{"n"}","&:",b)}$0=a":"b'
You can change the m and n to desired value (error handling was not done in my one-liner). If we do a test:
$ awk -v m="8" -v n="2" '{a=substr($0,1,m);b=substr($0,m+1);gsub(".{"n"}","&:",b)}$0=a":"b'<<<"aaaaaaaaaaaaaaa"
aaaaaaaa:aa:aa:aa:a
This might work for you (GNU sed):
sed 's/../&:/4g' file
Insert a : after the 4th occurrence of 2 characters and then globally throughout the line.

how do I use sed to delete lines with single digit instead of double

I have this line.
sed -i '/Total number 1/d' /tmp/test.txt
This will delete lines,
Total number 1
but it also deletes,
Total number 11
Total number 12
Total number 13
how do I set it to delete single digit only?
Add a dollar sign to the end sed -i '/Total number 1$/d' /tmp/test.txt
Also, if you want to delete any single digit, replace 1: sed -i '/Total number [0-9]$/d' /tmp/test.txt
Finally, if the number isn't necessarily at the end of the line, you could also have the pattern end when either the end of line or a non-digit is found: sed -i -E '/Total number [0-9]($|[^0-9])/d' /tmp/test.txt
The precise and generic solution would be:
sed '/\b[[:digit:]]\b/d'
\b stands for a word boundary.
Pass the -i option once you made sure that the above command works for you since it would effectively change your input files.

SED Command to remove first digits and spaces of each line

I have a simple text file in below format.
1 12658003Y
2 34345345N
3 34653785Y
4 36452342N
5 86747488Y
6 34634543Y
so on
10 37456338Y
11 33535555Y
12 37456378Y
so on
100 23432434Y
As you can see there are two white spaces after first number.
I'm trying to write SED command to remove the digits before whitespaces. Is there any SED command to remove spaces and number before spaces?
Output file should look like below.
12658003Y
34345345N
34653785Y
36452342N
so on..
Please assist. I'm very new to shell scripting.
sed 's/[0-9]\+\s\+//' infile > outfile
Explanation:
s: we want to use substitution
/: mark start and end of the expression we want to match
[0-9]: match any digit
+: match the previous one or more time
\s: space
+: match the previous one or more time
/: mark start of what we want to change our matches to (which is nothing)
/: some special operators goes after this (we use no such)
infile: the file we want to change
>: pipe stdout to
outfile: where we want to store output
Your sed command would be,
sed 's/.* //g' file
This would remove the first numbers along with the space followed.
Remove leading digits, then following spaces:
sed 's/^[0-9]* *//' file
sed 's/^[0-9]*[ ]*//g' input.txt

sed delete remaining characters in line except first 5

what would be sed command to delete all characters in line except first 5 leading ones, using sed?
I've tried going 'backwards' on this (reverted deleting) but it's not most elegant solution.
This might work for you (GNU sed):
echo '1234567890' | sed 's/.//6g'
12345
Or:
echo '1234567890' | cut -c-5
12345
Try this (takes 5 repetitions of 'any' character at the beginning of the line and save this in the first group, then take any number of repetition of any characters, and replace the matched string with the first group):
sed 's/^\(.\{5\}\).*/\1/'
Or the alternative suggested by mouviciel:
sed 's/^\(.....\).*/\1/'
(it is more readable as long as the number of first characters you want does not grow too large)

regular expression in sed for masking credit card

We need to mask credit card numbers.Masking all but last 4 digits. I am trying to use SED. As credit card number length varies from 12 digits to 19,I am trying to write regular expression.Following code will receive the String. If it contains String of the form "CARD_NUMBER=3737291039299199", it will mask first 12 digits.
Problem is how to write regular expression for credit card-12 to 19 digits long? If I write another expression for 12 digits, it doesn't work.that means for 12 digit credit card- first 8 digits should be masked. for 15 digit credit card, first 11 digits should be masked.
while read data; do
var1=${#data}
echo "Length is "$var1
echo $data | sed -e "s/CARD_NUMBER=\[[[:digit:]]\{12}/CARD_NUMBER=\[\*\*\*\*\*\*\*\*/g"
done
How about
sed -e :a -e "s/[0-9]\([0-9]\{4\}\)/\*\1/;ta"
(This works in my shell, but you may have to add or remove a backslash or two.) The idea is to replace a digit followed by four digits with a star followed by the four digits, and repeat this until it no longer triggers.
This does it in one sed command without an embedded newline:
sed -r 'h;s/.*([0-9]{4})/\1/;x;s/CARD_NUMBER=([0-9]*)([0-9]{4})/\1/;s/./*/g;G;s/\n//'
If your sed doesn't have -r:
sed 'h;s/.*\([0-9]\{4\}\)/\1/;x;s/CARD_NUMBER=\([0-9]*\)\([0-9]\{4\}\)/\1/;s/./*/g;G;s/\n//'
If your sed needs -e:
sed -e 'h' -e 's/.*\([0-9]\{4\}\)/\1/' -e 'x' -e 's/CARD_NUMBER=\([0-9]*\)\([0-9]\{4\}\)/\1/' -e 's/./*/g' -e 'G' -e 's/\n//'
Here's what it's doing:
duplicate the number so it's in pattern space and hold space
grab the last four digits
swap them into hold space and the whole number into pattern space
grap all but the last four digits
replace each digit with a mask character
append the last four digits from hold space to the end of the masked digits in pattern space (a newline comes along for free)
get rid of the newline
try this, you don't have to create complicated regex
var1="CARD_NUMBER=3737291039299199"
IFS="="
set -- $var1
cardnumber=$2
echo $cardnumber | awk 'BEGIN{OFS=FS=""}{for(i=1;i<=NF-4 ;i++){ $i="*"} }1'
output
$ ./shell.sh
************9199
I'm not much of a sed guru, and thus I cannot manage to do it in only one command, though there surely are ways. But with two sed commands, here is what I got:
sed -e 's/CARD_NUMBER=\([0-9]*\)\([0-9]\{4\}\)/\1\
\2/' | sed -e '1s/./x/g ; N ; s/\n//'
Please note the embedded newline.
Because sed works by lines, I first break the card number into the initial part and the last four digits, separating them by a newline (the first sed command). Then, I mask the initial part (1s/./x/g), and remove the new line (N ; s/\n//).
Good luck!