-1

I have a file with country names and then the mobile country code prefix for that country like this:

United Kingdom   +44  ...more fields
United States Virgin Islands +1  ...more fields

I need to get the country name and the mobile prefix. So the regular expression reads like: read all words from the beginning of the line and then a string that starts with a plus and one more more digits and then stop.

I tried a few things with grep, cut, and even sed but I can't get it.

  • What character delimits the fields/words of the data? Space or tab? – Kusalananda May 29 at 10:01
  • 1
    It's a space delimited file – C.S May 29 at 10:09
  • If you can use perl you could use something like this: cat /tmp/b.log | perl -ne '/(United.*)(\+[0-9]{1,2})/ && print ("1:" . $1 . "2: ". $2 . "\n")'. This assumes that /tmp/b.log is your file. This prints: 1:United Kingdom 2: +44 <CR> 1:United States Virgin Islands 2: +1 – mnille May 29 at 10:30
  • @mnille perl is too brutal for me. – C.S May 29 at 11:37
4

try with:

grep -o '^[^+]*+[0-9]\+' infile

[^+]*+ matches everything until first + found followed by one-or-more digits [0-9]\+

  • This will return the whole line but I want to do is extract the country and the mobile prefix. That's why I tried to use cut – C.S May 29 at 10:10
  • 1
    @C.S Using GNU grep, the given command does exactly what you seem to want to do on my machine. What Unix are you using? – Kusalananda May 29 at 10:25
  • No, it works. Cheers – C.S May 29 at 10:29
1

Command line:

$ perl -lne 'print /^(.*?\+\d+)/' input.txt

This will get you the desired info from the input.

Working:

  • /^(.*?\+\d+)/ regex shall extract everything upto the first occurrence of a plus symbol followed by atleast one digit symbol.
  • Since this is hooked to the BOL via the ^ caret symbol, so obviously only one matching can happen.
  • The matching is passed on to the print function.
  • -lne options are to run Perl code given in the -e option on each line of the input.file. -n makes perl not to print anything unless specifically asked to.

Output:

United Kingdom   +44
United States Virgin Islands +1
1

Since you say space is the delimiter used in your file, you should be able to use

cut -d ' ' -f 1-5 filename

to extract the five first columns from the file.

For the given data, this produces

United Kingdom   +44
United States Virgin Islands +1

... but I'm assuming that this is only by luck, as a country with more than two words in its name would span multiple fields (not just two).

A more robust method using sed to delete everything after the first set of digits on each line (i.e., everything after the country code):

sed 's/\([[:digit:]][[:digit:]]*\).*/\1/' filename

or,

sed 's/\([[:digit:]]\{1,\}\).*/\1/' filename

or, with -E and an extended regular expression instead,

sed -E 's/([[:digit:]]+).*/\1/' filename
  • Or sed 's/\([[:digit:]]\{1,\}\).*/\1/', \{1,\} being the standard BRE equivalent of ERE +. – Stéphane Chazelas Jun 6 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.