Displaying text till pattern match found in a line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Displaying text till pattern match found in a line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-31-2013
Optimus81 Optimus81 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 17 May 2014, 6:04 PM EDT
Posts: 67
Thanks: 21
Thanked 0 Times in 0 Posts
Displaying text till pattern match found in a line

Hi All,
From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able
to print text before the pattern. it display texts which is found after pattern.

Code:
awk '/[Pp]assed/{print;getline;print}' file_name | sed 's/^[^:]*. *//'

input line from the file_name looks like

Code:
U12-C02: Software checks is passed. (Latest Software version : IED-2011-S : upgrade_01 4.13.2 (4.10.15_TREP-RT_13.31))

Currently it is displaying like this :

Code:
Software checks is passed. (Latest Software version : IED-2011-S : upgrade_01 4.13.2 (4.10.15_TREP-RT_13.31))

expected output should be like this :

Code:
U12-C02: Software checks is passed.

Many Thanks,
Optimus
Sponsored Links
    #2  
Old 01-31-2013
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 19 September 2014, 2:11 PM EDT
Location: The Netherlands
Posts: 7,716
Thanks: 123
Thanked 537 Times in 507 Posts

Code:
awk -F"." '/[Pp]assed/{print $1 FS}' file

The Following User Says Thank You to Franklin52 For This Useful Post:
Optimus81 (01-31-2013)
Sponsored Links
    #3  
Old 01-31-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
 
awk '{for(i=1;i<=NF;i++){if($i ~ /[pP]assed/){s=s?s" "$i:$i; print s;s="";next}else{s=s?s" "$i:$i;}}}' file

    #4  
Old 01-31-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 September 2014, 7:30 AM EDT
Location: NM
Posts: 10,211
Thanks: 278
Thanked 796 Times in 743 Posts

Code:
awk -F '.'   '/[Pp]assed/{print $1 ;getline;print $1}' file_name

If you display several line of text from you input file, maybe we can do better than just guess as to what we we're reading. I do not see why you have getline in there. Your expected output is just a single line, per your example.
Sponsored Links
    #5  
Old 01-31-2013
Optimus81 Optimus81 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 17 May 2014, 6:04 PM EDT
Posts: 67
Thanks: 21
Thanked 0 Times in 0 Posts
Thanks Franklin. It works fine.
Sponsored Links
Closed Thread

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
To add a new line with specific text after the pattern is found using sed smarlaku Shell Programming and Scripting 1 01-11-2013 02:27 AM
Pattern match till the end of the file. halfafringe Shell Programming and Scripting 2 07-06-2012 04:47 AM
Grep the word from pattern line and update in subsequent lines till next pattern line reached rbalaj16 Shell Programming and Scripting 5 06-18-2012 04:39 AM
Print characters till the next space when the pattern is found gpk_newbie Shell Programming and Scripting 2 10-19-2011 05:02 AM
Awk script to match pattern till blank line justchill Shell Programming and Scripting 7 11-19-2010 03:33 AM



All times are GMT -4. The time now is 12:13 PM.