Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Displaying text till pattern match found in a line

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-31-2013
Optimus81 Optimus81 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 20 November 2014, 4:13 PM EST
Posts: 69
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 Unix and Linux 01-31-2013
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 1 September 2015, 9:23 AM EDT
Location: The Netherlands
Posts: 7,732
Thanks: 129
Thanked 549 Times in 516 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 Unix and Linux 01-31-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 9 July 2015, 10:56 AM EDT
Posts: 1,650
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 Unix and Linux 01-31-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 2 September 2015, 4:18 PM EDT
Location: NM
Posts: 10,532
Thanks: 353
Thanked 881 Times in 819 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 Unix and Linux 01-31-2013
Optimus81 Optimus81 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 20 November 2014, 4:13 PM EST
Posts: 69
Thanks: 21
Thanked 0 Times in 0 Posts
Thanks Franklin. It works fine.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:52 AM.