Getting value of a pattern preceding another pattern


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting value of a pattern preceding another pattern
# 1  
Getting value of a pattern preceding another pattern

I have a file like this
-------------------------------
Code:
[2012-11-12 :  11:59
 some text
 some moe text
  ERR-XXXXX
soe more text
]
[2012-11-12 : 12:00 blala blah blah]
[2012-11-12 :  12:01
 some text
 some moe text
  ERR-XXXXX
soe more text
]
[2012-11-12: 12:02  some other text ]

--------------------------------------

I need a way to find the timestamp preceding the ERR-XXXXX

Here XXXX deonoes any any numeric string 00000-99999
that is a output like this

Code:
2012-11-12 :  11:59-ERR-XXXXX
2012-11-12 :  12:01-ERR-XXXXX


Last edited by Scrutinizer; 11-16-2012 at 06:49 AM.. Reason: code tags
# 2  
Code:
awk '/\[2012/{a=$0}/^ERR/{print a"-"$0}' input.txt
[2012-11-12 : 11:59-ERR-XXXXX
[2012-11-12 : 12:01-ERR-XXXXX

---------- Post updated at 10:01 AM ---------- Previous update was at 09:59 AM ----------

Code:
awk '/\[2012/{sub("\\[","",$0);a=$0}/^ERR/{print a"-"$0}' input.txt
2012-11-12 : 11:59-ERR-XXXXX
2012-11-12 : 12:01-ERR-XXXXX

This User Gave Thanks to itkamaraj For This Post:
# 3  
the command is working in the shell.
However when calling this same command from a function
$0 is getting replaced by KSH and the date part is not coming only ERR part is coming

like this
Code:
-ERR-XXXXX some more text
-ERR-XXXXX some other text


Last edited by Scrutinizer; 11-16-2012 at 06:59 AM.. Reason: code tags
# 4  
can you show your function and how you are calling the function?
# 5  
Code:
myFunc(){
echo "In Function ...processing $1"

echo "awk '/\[$1/{sub("\\[","",$0);a=$0}/^ORA/{print a"-"$0}'" 
awk '/\[$1/{sub("\\[","",$0);a=$0}/^ERR/{print a"-"$0}' $2.log>>$3 

awk '/\[$1/{sub("\\[","",$0);a=$0}/^ERR/{print a"-"$0}' $2_$4>>$3 

awk '/\[$1/{sub("\\[",""$0);a=$0}/^ERR/{print a"-"$0}' $2_$5>>$3 
 
}

Calling
Code:
myFunc "$currentMonth\/$currentDay\/$currentYearYY" SystemOut $inputFile "$currentYearYY.$currentMonth0.$currentDay0*"  $prevDayYr.$prevDayMonth.$prevDayDay


Last edited by Scrutinizer; 11-16-2012 at 06:50 AM.. Reason: code tags
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #119
Difficulty: Easy
The Single UNIX Specification (SUS) is a standard for Unix systems to qualify and have permission to use the UNIX certification mark.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

5. 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

6. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

7. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

8. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

9. Shell Programming and Scripting

Log File - Getting Info about preceding Date of Pattern Found

Ok Suppose I have a log file like the below: 2010-07-15 00:00:01,410 DEBUG 2010-07-15 00:01:01,410 DEBUG 2010-07-15 00:01:02,410 DEBUG com.af ajfajfaf affafadfadfd dfa fdfadfdfadfadf fafafdfadfdafadfdaffdaffadf afdfdafdfdafafd error error failure afdfadfdfdfdf EBUDGG eafaferror failure... (6 Replies)
Discussion started by: SkySmart
6 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

Featured Tech Videos