0

i have a string in text file, i want to grep the email address but it is not working

26,2,12,1,1,0,1,0,12,20,648130864.573339,,"iCloud - Device Locator",,,69FFBE2F-6626-4A4B-8A78-BF7D06DE59C7,7F223B27-9588-4719-81B9-074666A76E1D,com.apple.accountsd,[email protected],

i try: grep -Eo "\b@'[^']*'" but it not working

i need output: [email protected]

P/s: maybe the field isn't 19th but email is always at the end of the line

4
  • 1
    You've said nothing about the properties of this text string or whether that's the only thing in the file. For example, is the string a properly quoted CSV record, always? If not, might there be embedded commas in some field so that the email is not strictly in the 19th field? If so, will the email address always be in the second to last field?
    – Kusalananda
    Jul 16, 2021 at 13:32
  • email is always at the end of the line Jul 16, 2021 at 13:40
  • 2
    but in your example there is another comma in the end... that means it would be the second to last field.
    – pLumo
    Jul 16, 2021 at 13:40
  • grep -o '[^,]*@[^,]*'
    – nezabudka
    Jul 16, 2021 at 22:11

5 Answers 5

3

Use csvcut from csvkit:

csvcut -c 19 file

Alternatively, you might try want to try awk if it is always the second to last field:

awk -F, '{print $(NF-1)}'

This won't work if the last field contained a comma.

2

If the email is always the 19th item, you can use

cut -d, -f 19

-d, means ',' is the delimiter and -f 19 means you want the 19th field.

Update:

If the email is always the penultimate item on the line (note that you have a comma after it), you can use sed:

sed -e 's/.*,\(.*\),/\1/'
5
  • it working but Email is random, can there be 1 command for all cases? Jul 16, 2021 at 13:36
  • what do you mean "is random" ?
    – pLumo
    Jul 16, 2021 at 13:38
  • maybe the field isn't 19th (random) but email is always at the end of the line Jul 16, 2021 at 13:42
  • sed -e 's/.*,\(.*\),/\1/'
    – Zé Loff
    Jul 16, 2021 at 13:44
  • Thank You very much, it's working: sed -e 's/.*,(.*),/\1/' Jul 16, 2021 at 13:51
1

Remove the last comma on the line with a sed. Then remove everything before and including the last comma, this leaves the email address, as follows:

XXXX@Atlas:~$ echo "26,2,12,1,1,0,1,0,12,20,648130864.573339,,"iCloud - Device Locator",,,69FFBE2F-6626-4A4B-8A78-BF7D06DE59C7,7F223B27-9588-4719-81B9-074666A76E1D,com.apple.accountsd,[email protected]," | sed "s/,$//g; s/^.*,//g"
[email protected]
1

Using Raku (formerly known as Perl_6):

raku -ne 'for .split(",") {.put if m:g/ \@ /;};' 

OUTPUT:

[email protected]

The :g 'global' adverb allows for more that one match. If you're absolutely certain of the desired field, you could drop the m/.../ match and add an index (e.g. [18]):

raku -ne '.put for .split(",")[18];'

OUTPUT:

[email protected]

https://raku.org/

1

Using cut by reversing the input, so that the email becomes the second comma delimited field, and finally reverse once more:

< file rev | cut -d, -f2 | rev 
[email protected]

Using comma as field separator:

perl -F, -pale '$_ = pop @F' < file

turn commas to newlines then keep chopping until we're left with one newline

sed -n 'y/,/\n/;/\n.*\n/D;P' < file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .