Need help on pattern matching and printing the same


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on pattern matching and printing the same
# 1  
Old 05-18-2017
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
# 2  
Old 05-18-2017
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 05-18-2017
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
Quote:
Originally Posted by Sumanthsv
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
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. UNIX for Dummies Questions & Answers

Find pattern suffix matching pattern

Hi, I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts. /tmp/1 IP=123.456.789.01 WAS_HOSTNAME=abcdefgh.was.tb.dsdc /tmp/hosts 123.456.789.01 I want this result in /tmp/hosts if hostname is already there dont want duplicate entry. ... (5 Replies)
Discussion started by: rajeshwebspere
5 Replies

6. Shell Programming and Scripting

Help With AWK Matching and Re-printing Lines

Hi All, I'm looking to use AWK to pattern match lines in XML file - Example patten for below sample would be /^<apple>/ The sample I wrote out is very basic compared to what I am actually working with but it will get me started I would like to keep the matched line(s) unchanged but have them... (4 Replies)
Discussion started by: rhoderidge
4 Replies

7. Shell Programming and Scripting

Pattern Matching and printing

Dear All, I have a log file like below 13:26:31 |152.22 13:27:31 |154.25 13:28:31 |154.78 13:29:31 |151.23 13:30:31 |145.63 13:31:31 |142.10 13:32:31 |145.45 where values will be there from 00:00 hrs to 23:59 hrs. I'm matching for last occurance of 23:59 and printing 1440 lines (grep... (4 Replies)
Discussion started by: Naga06
4 Replies

8. UNIX for Dummies Questions & Answers

Pattern matching and Printing Filename

Hi, My requirement is to search for a paritcular string from a group of .gz files and to print the lines containing that string and the name of the files in which that string is present. Daily 500 odd .gz files will be generated in a directory(directory name will be in the form of... (4 Replies)
Discussion started by: krao
4 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question