How to remove text between two expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove text between two expressions
# 1  
Old 10-13-2011
Question How to remove text between two expressions

I need to remove a section of text on a line that is between two patterns.
This is the line:
Code:
Begin   : Transaction Id 00004d9e769065ab 04-Oct-2011 04:01:48.69                       Username smith

I need the line to read:
Code:
Begin   : 04-Oct-2011 04:01:48.69                       Username smith


I then need to add some text/chars so the line will read as follows:
Code:
Begin   : Datetime [04-Oct-2011 04:01:48.69],  Username [smith]

I think I can get the last line once I get the text removed that I need out of there. I need help getting the ']' at the end though.

Thank you !!!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-13-2011 at 06:18 PM.. Reason: code tags, please!
# 2  
Old 10-13-2011
Code:
echo 'Begin   : Transaction Id 00004d9e769065ab 04-Oct-2011 04:01:48.69                       Username smith' | nawk '{print $1,$2, "Datetime [" $(NF-3) " " $(NF-2) "],", $(NF-1) " [" $NF "]"}' OFS="\t"

# 3  
Old 10-17-2011
thank you, that works for the line but I need to run it through an entire transaction log file and change all of the lines that have 'Begin ' to the format I outlined above.
# 4  
Old 10-17-2011
Quote:
Originally Posted by deneuve01
thank you, that works for the line but I need to run it through an entire transaction log file and change all of the lines that have 'Begin ' to the format I outlined above.
You didn't post a sample of the entire log file...
This is just a guess...
Code:
nawk '/^Begin/ {print $1,$2, "Datetime [" $(NF-3) " " $(NF-2) "],", $(NF-1) " [" $NF "]"}' OFS="\t" myLogFile

# 5  
Old 10-18-2011
Thank you ! Unfortunately the code stripped everything but the "Begin' lines.

This is what I should have posted the very first time - sorry for the confusion.


The log below >>>
Code:
Begin   : Transaction Id 00004d9e769065ab 04-Oct-2011 04:01:48.69                       Username smith
  data data data data      
End     : Transaction Id 00004d9e7690665c 04-Oct-2011 04:01:49.57
data data data data
Insert  : Transaction Id 00004d9e7690665a Id (22,0)                   Table [machines,smith]
data data data data data 
Update : Transaction Id 00004d9e777d2d1b Id (312441,0)                                Table [testsystem,smith]
data data data data 
Delete : Transaction Id 00004d9e777ba197 Id (1,0)                     Table [system,smith]
data data data data

Needs to look like this log >>>>
Code:
Begin   :  Datetime [04-Oct-2011 04:01:48.69], [Username smith]
  data data data data      
End     : Datetime [04-Oct-2011 04:01:49.57]
data data data data
Insert  :     Table [machines,smith]
data data data data data 
Update :  Table [testsystem,smith]
data data data data 
Delete : Table [system,smith]
data data data data

Thank you !

Last edited by vgersh99; 10-18-2011 at 12:54 PM.. Reason: code tags, please!
# 6  
Old 10-18-2011
Code:
nawk '/^Begin/ {print $1,$2, "Datetime [" $(NF-3) " " $(NF-2) "],", $(NF-1) " [" $NF "]";next}$1 ~ /Insert|Update|Delete/ {$0=$1 OFS $2 OFS $(NF-1) OFS $NF}1' OFS='\t' myFile

# 7  
Old 10-18-2011
Thank you ! I think that took care of everything but the 'End' line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove the text between all curly brackets from text file?

Hello experts, I have a text file with lot of curly brackets (both opening { & closing } ). I need to delete them alongwith the text between opening & closing brackets' pair. For ex: Input:- 59. Rh1 Qe4 {(Qf5-e4 Qd8-g8+ Kg6-f5 Qg8-h7+ Kf5-e5 Qh7-e7+ Ke5-f5 Qe7-d7+ Qe4-e6 Qd7-h7+ Qe6-g6... (6 Replies)
Discussion started by: prvnrk
6 Replies

2. Shell Programming and Scripting

Bash script - coloring reg. expressions in text

Hi all, is there anyone good at bash who will help me? I need to use syntax ${string/pattern/replacement} The problematic part where I am stuck is: #!bin/bash text="A cat is on a mat." exp="cat" newexp="SOMECODEcatSOMECODE" newtext=${${text}/${exp}/${newexp}} == > ERROR "wrong... (4 Replies)
Discussion started by: JohnnyM77
4 Replies

3. Shell Programming and Scripting

Remove ending text

Hello, I am working with a list that contains a large number of files listed by their absolute path. I am trying to determine a way to delete the file name at the end of each line, therefore leaving just the directory path. For example, I'd like to go from: /home/something/file... (2 Replies)
Discussion started by: omnivir
2 Replies

4. Shell Programming and Scripting

Remove text between brackets

How can I use bash to remove all text between "<" and ">" in a file? (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

Remove characters from text

I have a file which looks like this. I only show first 11 lines of the file followed by some text that appears at the end of every file. 1. file:///path1/path2/path3/path4/251192.dat (score 3.849384, docid 142923) 2. file:///path1/path2/path3/path4/173859.dat (score 3.831033, docid 75365) 3.... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

6. Shell Programming and Scripting

remove specified line from text

hi, I wish to move a specified line from a text file: ltoremove=5 #remove 5th line i=1 while read line ; do if ; then ... i=$(($i+1)) done < "txt.txt" how should this actually be done? is there a shorter faster way to do this? :wall: Thanks, Ted (2 Replies)
Discussion started by: ted_chou12
2 Replies

7. UNIX for Dummies Questions & Answers

Remove text in line

Hi, i have a text file with some links and i want to remove some characters from it. My text file is something like this: <p> <b> Arquivo CASC2270.09o transferido com sucesso. </b> </p><p> <b> Verificando CASC2270.09o ... </b> </p><p> <b> Iniciando processamento de CASC2270.09o ... </b>... (11 Replies)
Discussion started by: limadario
11 Replies

8. Shell Programming and Scripting

How to extract text from string using regular expressions

Hi, I'm trying to use sed to extract some text and assign it to a variable. Can anyone provide me with some help? it would be much appreciated! I"m looking to extract for example: filename=/output/R34/2005_13_R34_C1042S_T83_CRFTXT_20081015.txt I'm trying to extract the 1042... (9 Replies)
Discussion started by: jtung
9 Replies

9. Shell Programming and Scripting

remove specified text from file

I am trying to write a script that kills old sessions, I've posted here over the past few days and the script is just about perfect except I want to be given the option to exclude specified PIDs from being killed. this is the entire script: if then rm /tmp/idlepids fi if then rm... (2 Replies)
Discussion started by: raidzero
2 Replies

10. Shell Programming and Scripting

Remove duplicate text

Hello, I have a log file which is generated by a script which looks like this: userid: 7 starttime: Sat May 24 23:24:13 CEST 2008 endtime: Sat May 24 23:26:57 CEST 2008 total time spent: 2.73072 minutes / 163.843 seconds date: Sat Jun 7 16:09:03 CEST 2008 userid: 8 starttime: Sun May... (7 Replies)
Discussion started by: dejavu88
7 Replies
Login or Register to Ask a Question