Replace my perl with awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace my perl with awk or sed
# 1  
Old 07-13-2017
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
# 2  
Old 07-13-2017
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..
# 3  
Old 07-14-2017
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 07-14-2017
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...
# 5  
Old 07-14-2017
Code:
awk '{for (i=1; i<=NF; i++) {w=$i; sub(".*[.]", ".", w) ; if (ext "$" ~ w) print $i}}' ext=".bin,.exe" list_of

# 6  
Old 07-14-2017
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..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-14-2017
Quote:
Originally Posted by rdrtx1
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
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..

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace using sed or awk

Hi, Need a help to replace a word if a pattern is found between the delimiters preferably using SED or AWK. below is the sample file that iam dealing with, need to match pattern 'application' if found replace the whole word between the delimiters and also print the lines that don't match.... (1 Reply)
Discussion started by: tech_frk
1 Replies

2. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

3. Shell Programming and Scripting

replace using pattern using awk or sed

I have file file_1.txt which contains: file_1.txt <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">a</td>... (2 Replies)
Discussion started by: sol_nov
2 Replies

4. Shell Programming and Scripting

sed or awk to replace a value in a certain line.

I have an input like following. *DEFINE_CURVE_TITLE Force for tool binder $# lcid sidr sfa sfo offa offo dattyp 3 0 1 .000000 125.00000 0.000 0.000 0 $# a1 ... (5 Replies)
Discussion started by: hamnsan
5 Replies

5. Shell Programming and Scripting

perl or sed replace lines only if for all files

Hi Everyone, # cat 3 a b # cat 4 a b # perl -p -i -e "s/.*/c/ if $.==2" * # cat 3 a c # cat 4 a b (4 Replies)
Discussion started by: jimmy_y
4 Replies

6. UNIX for Dummies Questions & Answers

replace 0.00 with awk/sed

Hi I have a problem when i use awk or sed to replace characters in file. For example when I want to replace line like this : 00000O120100512 1.70 1.59 0.00 +7.280 I want to get a new line : 0000000O120100512 1.70 1.59 13.56 +7.280 In ksh : awk... (1 Reply)
Discussion started by: Artur
1 Replies

7. Shell Programming and Scripting

perl replace awk strftime

Hi Everyone i have a perl file below, one of the line is convert the pcho time to human readable format. $value=`awk 'BEGIN{print strftime("%c",1273236600)}' | tr -d '\n'`; if image, if i have lots of pcho time value in a file, if i use this awk, strftime, then tr -d to remove the \n,... (2 Replies)
Discussion started by: jimmy_y
2 Replies

8. Shell Programming and Scripting

Sed help to replace and then awk

Sed help echo "(200 rows affected)" | sed -e '/\(//p' | sed -e '/\)//p' | awk '{print $1}' I want output as "200" Please help me correct (2 Replies)
Discussion started by: pinnacle
2 Replies

9. UNIX for Dummies Questions & Answers

how to use sed or perl command to find and replace a directory in a file

how to use sed command to find and replace a directory i have a file.. which contains lot of paths ... for eg.. file contains.. /usr/kk/rr/12345/1 /usr/kk/rr/12345/2 /usr/kk/rr/12345/3 /usr/kk/rr/12345/4 /usr/kk/rr/12345/5 /usr/kk/rr/12345/6 /usr/kk/rr/12345/7... (1 Reply)
Discussion started by: wip_vasikaran
1 Replies

10. Shell Programming and Scripting

Replace tr/sed with awk

There is a service that runs that we call multi-streaming that calls a shell script multiple times simultaneously. In this shell script is the following line: tr '\r' '\n' < $POLLFILE.OUT | sed '/0000000000000016000A/d' > $POLLFILE When I run this manually it produces the desired results, but... (6 Replies)
Discussion started by: philplasma
6 Replies
Login or Register to Ask a Question