Print character after pattern found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print character after pattern found
# 1  
Old 07-25-2013
Print character after pattern found

Hi Gurus, i need your help to create a script the will print a characters after the pattern was found.
Sample lines are below:

Code:
My birthday:"1977-16-07", My birthday:"1975-16-07"
My birthday:"1970-16-07"

.

My patter should be "birthday:", then i want to print the following characters which is the complete date: Note( some lines has multiple occurence of the patters)
So my desired output would be:
Code:
1977-16-07
1975-16-07
1970-16-07

appreciate your help. Thanks
Moderator's Comments:
Mod Comment I added CODE tags, but I am not convinced that they are in the right spot.

Last edited by Don Cragun; 07-25-2013 at 12:24 AM.. Reason: CODE tags
# 2  
Old 07-25-2013
Perl way...there are many ways to do it with awk and grep as well...

Code:
$ perl -nle '$,="\n";@array = /birthday:\"(\d{4,4}-\d{2,2}-\d{2,2})\"/g; print @array' file
1977-16-07
1975-16-07
1970-16-07

# 3  
Old 07-25-2013
please provide some back ground information.Seems the query is not pretty clear to create a script.there can be multiple ways to do as per above statements but not yet clear what is the specific requirement
# 4  
Old 07-25-2013
you can try with awk and regex.
# 5  
Old 07-25-2013
In your sample, the pattern doesn't matter, and your statement specifying your requirements doesn't match the output you say you want (the double quotes are missing in the output).

To get the output you requested from the input you gave (if I guessed correctly about where the CODE tags should have been), you could try something simple like:
Code:
awk -F'"' '{for(i = 2; i < NF; i += 2) print $i}' file

If file contains your sample input, the output produced matches your desired output.

As always, if you're using a Solaris/SunOS system, you need to use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /bin/awk or /usr/bin/awk.
# 6  
Old 07-25-2013
Quote:
Originally Posted by rajamadhavan
Perl way...there are many ways to do it with awk and grep as well...

Code:
$ perl -nle '$,="\n";@array = /birthday:\"(\d{4,4}-\d{2,2}-\d{2,2})\"/g; print @array' file
1977-16-07
1975-16-07
1970-16-07


Thanks.. Can you show me how it is done in awk and grep? Thanks a lot
# 7  
Old 07-25-2013
scripter123,

Check this out:
Code:
awk '{A=split($0,a,/"/);for(i=1;i<=A;i++) if(a[i]~/birthday/) print a[i+1]}' file
1977-16-07
1975-16-07
1970-16-07

Enjoy ,Have fun!.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

2. Shell Programming and Scripting

Copy/print all lines between pattern is found in .log files

Hi, I have a folder with multiple (< 33) .log files. And I have to copy the lines between two patterns from all the .log files to a new file. (script file with a loop?) Thanks in advance. 1.log ... .. xx1> begin ... .. .. >>> Total: 2 Alarms .. .. (17 Replies)
Discussion started by: AK47
17 Replies

3. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

4. Shell Programming and Scripting

Print Specific lines when found specific character

Hello all, I have thousand file input like this: file1: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ | | | |$$ $$ UERT | TTYH | TAFE | FRFG |$$ $$______|______|________|______|$$ $$ | | | |$$ $$ 1 | DISK | TR1311 | 1 |$$ $$ 1 |... (4 Replies)
Discussion started by: attila
4 Replies

5. Shell Programming and Scripting

Print characters till the next space when the pattern is found

i have a file which contains alphanumeric data in every line. what i need is the data after certain pattern. the data after the pattern is not of fixed length so i need the data till the space after the pattern. Input file: bfdkasfbdfg khffkf lkdhfhdf pattern (datarequired data not required)... (2 Replies)
Discussion started by: gpk_newbie
2 Replies

6. Shell Programming and Scripting

print next word after found pattern

Hi all, I'd like to print the next word after a found pattern. example text: word1 word2 word3 word4 pattern word5 pattern word1 word2 word3 word4 word1 word2 pattern word4 basiclly the word after pattern. Thanks (9 Replies)
Discussion started by: stinkefisch
9 Replies

7. Shell Programming and Scripting

Print the 2nd line everytime after defined pattern is found.

Hi, I have a text file similar to the example below and I want to print the second line every time after the "--------------------------" pattern is found. The pattern is a fixed length of - characters. Example of input; 1 -------------------------- 2 3 39184018234 4 ... (10 Replies)
Discussion started by: lewk
10 Replies

8. Shell Programming and Scripting

Find a pattern and print next all character to next space

Hi, I have a big inventory file that is NOT sorted is any way. The file is have "tagged" information like the ip address "*IP=" or the name "*NM=" . How do I get just the ip address or the name and not the whole line? I have tried to use AWK without any success. I always get the whole line... (8 Replies)
Discussion started by: pierrebjarnfelt
8 Replies

9. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question