grep and sed to find a pattern and add newline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep and sed to find a pattern and add newline
# 1  
Old 10-14-2004
grep and sed to find a pattern and add newline

Hello All,
I have log file the result from a multithreaded process. So when a process finishes it will write to this log file as 123 rows merged.
The issue is sometimes the processess finish at the same time or write to the file at the same time as
123 rows merged.145 rows merged.
At the end of the script we do a source count from table and target count from log file. As you can see the above line will fail grep as grep will identify the line and move on while awk will take the first parameter as 123 only.
I have used the following command to correct this

grep -i "^[0-9].* rows merged" ttt | sed 's/merged\./merged\
/g' | grep -i "^[0-9].* rows merged" | sed '/^$/'d | awk '{print $1}' | while read X
do
echo $X
Y=`expr $Y + $X`
done
echo $Y
TCOUNT=${Y}
but there was another instance when this script failed because the log file has something like
Commit complete.150 rows merged.Commit complete.
The above 150 rows that were merged didn't get counted. I would like to find [0-9].* rows merged anywhere on any line and separate this pattern into a new line
EG: the above Commit complete.150 rows merged.Commit complete.
should become
Commit complete.
150 rows merged.
Commit complete.
Any help will be greatly appreciated.
Thanks
# 2  
Old 10-14-2004
Ok. Quit posting the same question twice. If people want to/are able to answer your question then they will. Be patient.
# 3  
Old 10-15-2004
Sorry didn't realize it was posted twice. I apologize. I already found a solution.
Thanks
Suresh
# 4  
Old 10-15-2004
Code:
awk '
  BEGIN{FS="."}
  {
    t=$0
    for(i=1 ; i <= NF; i++)
    {
        if ( match($i,"[0-9].* rows merged")> 0)
        {
            print $i
        }
    }
  } ' logfile

Try that as a start.
# 5  
Old 10-15-2004
Duplicate threads merged

Duplicate threads/posts merged.
# 6  
Old 10-26-2004
Bug

The awk script worked very fine. The other fix I had using grep and sed to filter what I need doesn't work very well. It atleast has a tendency to fail. But the awk works just fine.
Thanks
Suresh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk to find pattern and add priffix

Original File Server1|poweredOn|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Need Output Server1|poweredOn|DR|T1|268401| Server1/Server1.vmx|Red Hat Enterprise Linux 5 (64-bit) Conduction to check find the string "SFCHT1" and "SR" and add prefix has... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 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. UNIX for Dummies Questions & Answers

Mac OS X sed, add newline after pattern

Hi, I've been trying to work out how to add a new line to a file when the pattern matches .dmg. I've been searching Google but yet not found a working solution. Help would be appreciated... (9 Replies)
Discussion started by: pburge
9 Replies

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

7. Shell Programming and Scripting

[Solved] Find duplicate and add pattern in sed/awk

<Update> I have the solution: sed 's/\{3\}/&;&;---;4/' The thread can be marked as solved! </Update> Hi There, I'm working on a script processing some data from a website into cvs format. There is only one final problem left I can't find a solution. I've processed my file... (0 Replies)
Discussion started by: lolworlds
0 Replies

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. Shell Programming and Scripting

SED: how to remove newline after pattern?

Hi, I have the following XML not well-indented code: <hallo >this is a line </hallo> So I need to remove the newline. This syntax finds what I need to correct, but I don't know how to remove the newline after my pattern: sed 's/<.*$/&/' How can I subtract the newline after my... (1 Reply)
Discussion started by: nico.ben
1 Replies

10. Shell Programming and Scripting

grep the pattern followed by newline

Hi I have a problem using grep to find a pattern followed by newline. Here is my file xxxxxxxxxxxpattern patternxxxxxx pattern xxxpattern xxpatternx xxxxxxyyyyxxxx I want the result to be like this xxxxxxxxxxxpattern pattern xxxpattern Please help (2 Replies)
Discussion started by: lalelle
2 Replies
Login or Register to Ask a Question