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.

Need help on pattern matching and printing the same

Shell Programming and Scripting


Tags
solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-18-2017
Sumanthsv Sumanthsv is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 11 October 2017, 5:52 AM EDT
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Need help on pattern matching and printing the same

Hi,

I need to match for the pattern '.py' in my file and print the word which contains.

For example:


Code:
cat testfile

a b 3 4.py 5 6
a b.py c.py 4 5 6 7 8
1.py 2.py 3 4 5 6

Expected output:


Code:
4.py
b.py c.py
1.py 2.py

TIA
Sponsored Links
    #2  
Old 05-18-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 18 October 2017, 9:04 AM EDT
Location: Chennai
Posts: 2,630
Thanks: 579
Thanked 1,251 Times in 1,126 Posts
Hello Sumanthsv,

Could you please try following and let me know if this helps you.

Code:
awk '{for(i=1;i<=NF;i++){if($i ~ /.py/){printf("%s ",$i)}};print ""}'   Input_file

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
Sumanthsv (05-18-2017)
Sponsored Links
    #3  
Old 05-18-2017
Sumanthsv Sumanthsv is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 11 October 2017, 5:52 AM EDT
Posts: 17
Thanks: 7
Thanked 0 Times in 0 Posts
Thanks ..its working fine

---------- Post updated at 06:45 AM ---------- Previous update was at 06:41 AM ----------

Hi RavinderSingh13

If possible,Could you please explain this below part of code given by you.


Code:
{printf("%s ",$i)}};print ""}

    #4  
Old 05-18-2017
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 18 October 2017, 9:04 AM EDT
Location: Chennai
Posts: 2,630
Thanks: 579
Thanked 1,251 Times in 1,126 Posts
Quote:
Originally Posted by Sumanthsv View Post
Hi RavinderSingh13
If possible,Could you please explain this below part of code given by you.

Code:
{printf("%s ",$i)}};print ""}

Hello Sumanthsv,

Could you please go through following and let me know if this helps you.

Code:
awk '{
for(i=1;i<=NF;i++){                ##### Starting a for loop here which will start from variable i's value 1 to till number of field's value(NF).
if($i ~ /.py/){                    ##### Checking a condition here if any field's value is equal to .py, if yes then perform following action.
printf("%s ",$i)}                  ##### printing the value of that field which is having .py in it with space. NO new line.(so that all .py values on a single line I could print on a single line only)
};
print ""}                          ##### Printing NULL value, which means it will print a new line.
'  Input_file                      ##### mentioning the Input_file here.

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
Sumanthsv (05-18-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UNIX awk pattern matching and printing lines close2jay Shell Programming and Scripting 1 10-14-2015 06:50 AM
Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern essem Shell Programming and Scripting 11 06-05-2014 04:59 AM
Pattern Matching and printing Naga06 Shell Programming and Scripting 4 08-01-2011 10:05 AM
Pattern matching and Printing Filename krao UNIX for Dummies Questions & Answers 4 07-24-2009 12:04 AM
counting the lines matching a pattern, in between two pattern, and generate a tab d.chauliac Shell Programming and Scripting 4 03-19-2009 01:30 PM



All times are GMT -4. The time now is 09:07 AM.