Editing phone number with multiple delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing phone number with multiple delimiters
# 1  
Old 02-17-2013
Linux Editing phone number with multiple delimiters

Hello all
I have a data base of information that is formatted like so:
JSD4863 XXX-XX-XXXX DOE, JOHN C JR-II BISS CPSC BS INFO TECH 412/779-9445

I need the last four digits of the phone number. However, many lines contain
'garbage data' that I'm not interested in. So i used a 'for loop' to specify
which lines i wanted. The code looks like this:
Code:
#!/bin/bash
for linePosition in {11..22}
        do
                holder=`sed -n "${linePosition}p" $1|awk '{print $12}'` 
                echo "$holder"
        done

This gives me the 11 lines that I want. However, it gives me the full phone
number. I want to ALSO pipe this command to only give me the last 4 digits.
I am not sure how to do this since the fields are delimited by two different
characters. I tried a couple variations of cut, but nothing work.
Please help

Last edited by vgersh99; 02-17-2013 at 05:49 PM.. Reason: corrected code tags
# 2  
Old 02-17-2013
BASH itself lets you use multiple characters as the delimiter without the help of sed and awk.

Code:
while IFS=" -" read JSD AAA BB CCCC G
do
        echo "First col $JSD"
        echo "Second col $AAA"
        echo "Third col $BB"
        echo "Fourth col $CCC"
        echo "Fifth and later cols $G"
done < "$1"

# 3  
Old 02-17-2013
still confused

I'm sorry. im still pretty new to this stuff

i have no idea what your code does. I would like to just add a cut at the end. or pipe the phone number into a new command.
# 4  
Old 02-17-2013
It reads the entire file, one line at a time, into the variables listed. It uses IFS to change the shell's default delimiters. It will split on all characters listed in IFS.

Running the code would also give you a better idea what it does.

Running cut to edit individual lines is like making 300 phone calls to say one word apiece. Best to break that habit as early as possible.
# 5  
Old 02-17-2013
You could try this, fields are read into bash array name C starting with index 0 for field 1 (index 11 for field 12):

Code:
#!/bin/bash
sed -n "11,22p" $1 | while read -a C
do
    front=${C[11]%????}          # Front = Everything except last four chars of C[11] (field 12)
    holder=${C[11]#$front}       # Remove $front from front of C[12] and store in holder
    echo "$holder"
done

# 6  
Old 02-17-2013
If all you want is the last four digits of the phone numbers on lines 11 through 22 of your data file and the phone number is the last field on those 12 lines, a couple of simple ways to do that are:
Code:
#!/bin/bash
echo "Using awk:"
awk -F- '
NR < 11 {next}
NR > 22 {exit}
        {print $NF}' $1
echo "Using sed:"
sed -n '11,22s/.*-//p' $1

# 7  
Old 02-18-2013
Code:
awk '{ split($NF,a,"-"); print a[2]  }' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Auto phone number search

Hi. I want to search 10 phone numbers automatically in Facebook and store the result in some format. Can anyone help me with the script. I am using kali Linux. (2 Replies)
Discussion started by: looksthatmatter
2 Replies

2. Shell Programming and Scripting

Count The Number Of Delimiters using awk or better

What to know the way to count the number of delimiters in each record by ignoring the escape delimiters. Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"| I'm using awk -F'|' '{ print NF-1 }' command to find the number of delimiters. this command... (8 Replies)
Discussion started by: BrahmaNaiduA
8 Replies

3. Shell Programming and Scripting

awk multiple delimiters

Hi Folks, This is the first time I ever encountered this situation My input file is of this kind cat input.txt 1 PAIXAF 0 1 1 -9 0 0 0 1 2 0 2 1 2 1 7 PAIXEM 0 7 1 -9 1 0 2 0 1 2 2 1 0 2 9 PAKZXY 0 2 1 -9 2 0 1 1 1 0 1 2 0 1 Till the sixth column (which is -9), I want my columns to... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. Shell Programming and Scripting

Getting phone number, its message and assigning them into 2 variables then screen output.

Hi Everyone, I have a flatfile "inbox.txt" which contains some information: Location 0, folder "Inbox", SIM memory, Inbox folder SMS message SMSC number : "+24800000023" Sent : Sat 04 Aug 2012 09:01:00 PM +0700 Coding : Default GSM alphabet... (5 Replies)
Discussion started by: testcase
5 Replies

5. Shell Programming and Scripting

Concatinating the lines based on number of delimiters

Hi, I have a problem to concatenate the lines based on number of delimiters (if the delimiter count is 9 then concatenate all the fields & remove the new line char bw delimiters and then write the following data into second line) in a file. my input file content is Title| ID| Owner|... (4 Replies)
Discussion started by: bi.infa
4 Replies

6. Shell Programming and Scripting

Sorting problem: Multiple delimiters, multiple keys

Hello If you wanted to sort a .csv file that was filled with lines like this: <Ticker>,<Date as YYYYMMDD>,<Time as H:M:S>,<Volume>,<Corr> (H : , M, S: ) by date, does anybody know of a better solution than to turn the 3rd and 4th colons of every line into commas, sorting on four keys,... (20 Replies)
Discussion started by: Ryan.
20 Replies

7. Shell Programming and Scripting

AWK with multiple delimiters

I have the following string sample: bla bla bla bla bla I would like to extract the "123" using awk. I thought about awk -F"]" '{ print $1 }' but it doesn't work Any ideas ? (7 Replies)
Discussion started by: gdub
7 Replies

8. Shell Programming and Scripting

Perl script :- Phone number validation

Hi All, I am doing a perl script validation for Phone numbers. The normal phone number format is 01-32145. I need to do two validations for the phone number 1) A valid phone number can have at least two digits as prefix and at least five digits as postfix. e.g. 01-01011 2) A... (5 Replies)
Discussion started by: subin_bala
5 Replies

9. UNIX for Dummies Questions & Answers

Script to find the number of tab delimiters in a line

Hi, I need to find the number of tab delimiters in the first line of a file.So using word=`head -1 files.txt` I have extracted the first line of file into a variable word.It has 20 tab delimted columns.So can anyone help me in finding the number of delimiters? I am using csh and I am a... (5 Replies)
Discussion started by: poornimajayan
5 Replies

10. UNIX for Dummies Questions & Answers

How to count number of delimiters in a file name

I have a list of files with names as "FULL_abcd_xyz_timestamp.txt" and "FULL_xx_abcd_xyz_timestamp.txt". I am writing a script with a 'for loop' to take each file, strip the "FULL" and "timestamp" from the file name and do some actions on the contains of the file. So I need to know the number of... (4 Replies)
Discussion started by: ayanbiswas
4 Replies
Login or Register to Ask a Question

Featured Tech Videos