Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Replace my perl with awk or sed

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-13-2017   -   Original Discussion by invinzin21
invinzin21 invinzin21 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 17 November 2017, 1:48 PM EST
Location: Philippines, Cebu City
Posts: 123
Thanks: 16
Thanked 2 Times in 2 Posts
Replace my perl with awk or sed

My code below will print only the email address from all lines. I want to convert it with sed or awk.. also what if i just want to find only filenames.


Code:
cat LIS_EMAIL | perl -wne'while(/[\w\.\-]+@[\w\.\-]+\w+/g){print "$&\n"}'

Hoping to extract the filename such us .exe, .bin. From file that has scrambled lines.

Thanks
Sponsored Links
    #2  
Old Unix and Linux 07-13-2017   -   Original Discussion by invinzin21
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 21 November 2017, 9:38 AM EST
Location: Houston, Texas, USA
Posts: 983
Thanks: 0
Thanked 338 Times in 319 Posts

Code:
awk '{for (i=1; i<=NF; i++) {gsub(/[^A-Za-z0-9_@.-]/, "", $i); sub(/^[.]*/, "", $i); sub(/[.]*$/, "", $i); if ($i ~ /.@.+[.].+/) print $i}}' LIS_EMAIL


Last edited by rdrtx1; 07-13-2017 at 10:21 PM..
Sponsored Links
    #3  
Old Unix and Linux 07-14-2017   -   Original Discussion by invinzin21
invinzin21 invinzin21 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 17 November 2017, 1:48 PM EST
Location: Philippines, Cebu City
Posts: 123
Thanks: 16
Thanked 2 Times in 2 Posts
I want something like..


Code:
#cat list_of
ajsdfjasldfjasdflkasdfasdfsdaf klasjfklas aa.exe skdfjasdkljf
slkadjfaskljfsdfmsdf aa.bin akljdajs
lkajskjnnn  aaa sssss gggg 11111 bb.exe



# cat list_of|grep -E ".exe|bin"
aa.exe
aa.bin
bb.exe

    #4  
Old Unix and Linux 07-14-2017   -   Original Discussion by invinzin21
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 November 2017, 2:20 PM EST
Location: Aachen, Germany
Posts: 11,638
Thanks: 320
Thanked 3,618 Times in 3,323 Posts
So - do you want e-mail addresses and / or / xor file names? Seems you go for sort of MS style files? Please be aware that on *nix systems, ". extension" is ad libitum, and file names can contain nearly any character except the slash / . It may become difficult to identify the file names in a line...
Sponsored Links
    #5  
Old Unix and Linux 07-14-2017   -   Original Discussion by invinzin21
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 21 November 2017, 9:38 AM EST
Location: Houston, Texas, USA
Posts: 983
Thanks: 0
Thanked 338 Times in 319 Posts

Code:
awk '{for (i=1; i<=NF; i++) {w=$i; sub(".*[.]", ".", w) ; if (ext "$" ~ w) print $i}}' ext=".bin,.exe" list_of

Sponsored Links
    #6  
Old Unix and Linux 07-14-2017   -   Original Discussion by invinzin21
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 November 2017, 2:38 PM EST
Location: Amsterdam
Posts: 11,623
Thanks: 517
Thanked 3,380 Times in 2,979 Posts
If you have grep -o,

for the email addresses (even though a lot more characters are allowed, but this is what the perl says):

Code:
grep -Eo '[[:alnum:]_.-]+@[[:alnum:]_.-]+[[:alnum:]]+'

Filenames such as foo.exe or bar.bin:

Code:
grep -Eo '[[:alnum:]_.-]+\.(bin|exe)'


Last edited by Scrutinizer; 07-14-2017 at 03:06 PM.. Reason: Added underscores which are also word characters..
The Following User Says Thank You to Scrutinizer For This Useful Post:
invinzin21 (07-14-2017)
Sponsored Links
    #7  
Old Unix and Linux 07-14-2017   -   Original Discussion by invinzin21
invinzin21 invinzin21 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 17 November 2017, 1:48 PM EST
Location: Philippines, Cebu City
Posts: 123
Thanks: 16
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by rdrtx1 View Post
Code:
awk '{for (i=1; i<=NF; i++) {w=$i; sub(".*[.]", ".", w) ; if (ext "$" ~ w) print $i}}' ext=".bin,.exe" list_of

that doesnt work i tried it.
but thank you though.

---------- Post updated at 01:24 AM ---------- Previous update was at 01:20 AM ----------

Quote:
Originally Posted by Scrutinizer View Post
If you have grep -o,

for the email addresses (even though a lot more characters are allowed, but this is what the perl says):

Code:
grep -Eo '[[:alnum:].-]+@[[:alnum:].-]+[[:alnum:]]+'

Filenames such as foo.exe or bar.bin:

Code:
grep -Eo '[[:alnum:].-]+\.(bin|exe)'

It works.. Thank you so much

Code:
[@soctxadm01:~]$ grep .mml list_fi |wc -l
841
[@soctxadm01:~]$

[@soctxadm01:~]$  grep -Eo '[[:alnum:].-]+\.(mml)' list_fi|sort|uniq|wc -l
841


Code:
can you please explain the for me ..

 grep -Eo '[[:alnum:].-]+\.(mml) - character per character..

Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace [|] in perl irudayaraj Shell Programming and Scripting 5 05-07-2012 11:37 AM
How replace -- character in perl gvk25 Shell Programming and Scripting 1 04-19-2012 02:34 AM
perl find and replace fettie Shell Programming and Scripting 1 09-15-2011 10:05 AM
perl - replace value Shanks Shell Programming and Scripting 5 06-23-2011 04:28 PM
Replace Perl Module name in all Perl scripts rahulrathod Shell Programming and Scripting 2 12-02-2005 01:00 AM



All times are GMT -4. The time now is 06:52 PM.