Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bash substring not working how it should

Posted on 2011-10-25
5
Medium Priority
?
182 Views
Last Modified: 2012-05-12
Hi All,
I have a text file. The file contains some records out of which I wish to recover first 6 digits and last 4 digits.


$cat text.txt
111111****1111
222222****2222
333333****3333
444444****4444
555555****5555

I wrote a small shells script to locate records but it's not working.

while read inputline;
do
  card=$(echo "$inputline" | cut -d "," -f 1)
  D6=${card:1:6}                               ## last 4 digits 
  D4=${card:10:4}                             ## first 6 digits 
	if ! [[ "$D6" =~ ^[0-9]+$ ]]  || ! [[ "$D4" =~ ^[0-9]+$ ]]; then
	 echo "Inserted Card Number " $card $D6 $D4;
	      echo $count;
	else
	    echo "Card $card is invalid."
	fi
done < "$filename"

Open in new window


I get following results
D4 : 1111
D6 : 11111*
 
D4 : 2222
D6 : 22222*

Even if I change the line "D6=${card:1:6}"  to "D6=${card:0:6}"   there is no improvenment at all. What am I doing wrong?


Thanks in advance.
0
Comment
Question by:crazywolf2010
  • 4
5 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 37025581
Try ${card:0:5}
0
 
LVL 31

Expert Comment

by:farzanj
ID: 37025595
Sorry,

${card:0:6} should get the right result
0
 
LVL 31

Expert Comment

by:farzanj
ID: 37025633
The other problem is : ! in the if statement.  Do you really need that.

I ran your script and found the results as expected using ${card:0:6} and removing ! with the if statement
0
 
LVL 31

Accepted Solution

by:
farzanj earned 2000 total points
ID: 37025651
Try this:


#!/bin/bash

filename=XXXX

while read inputline;
do
  card=$(echo "$inputline" )
  D6=${card:0:6}                               ## last 4 digits
  D4=${card:10:4}                             ## first 6 digits
    if [[ "$D6" =~ ^[0-9]+$  ||  "$D4" =~ ^[0-9]+$ ]]; then
     echo "Inserted Card Number  $card $D6 $D4";
          echo $count;
    else
        echo "Card $card is invalid."
    fi
done < "$filename"

Open in new window

0
 

Author Closing Comment

by:crazywolf2010
ID: 37069989
Great work
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question