Insert tags which matches the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert tags which matches the pattern
# 1  
Old 07-14-2015
Insert tags which matches the pattern

Hi Guys,

How to achieve this in awk or sed:
Patterns: A.B. No. T-8346 or A.B. No. T-8xxx
will look like this:
Patterns: A.B. No. T-8346<br> or A.B. No. T-8xxx<br>

#cat file.txt
Code:
 [A.B. No. T-8346, January 01, 2015 ]
JHON VS. PETER, AGOO PET.

How Old Are You

the file will look like this:
Code:
 A.B. No. T-8346<br> January 01, 2015
JHON VS. PETER, AGOO PET.

How Old Are You

Thanks
# 2  
Old 07-14-2015
Hello lxdorney,

Following may help you in same but as per your given pattern of line [A.B. No. T-8346, January 01, 2015 ].
Code:
 awk '{if($0 ~ /^ \[A\.B/){gsub(/\[|\]/,X,$0);sub(/\,/,"<br>",$0)};print;next} 1'  Input_file

Output will be as follows.
Code:
  A.B. No. T-8346<br> January 01, 2015
JHON VS. PETER, AGOO PET.
How Old Are You

Thanks,
R. Singh
# 3  
Old 07-14-2015
Please be way more careful when preparing specifications! Your desired output does not match the verbal spec.
Where have the opening and closing brackets gone in your output? Where's the comma after the pattern?

And, any attempt from your side?
# 4  
Old 07-15-2015
Yes that's what we want to achieve, thank you so much..
if you could explain little by little much better to help other as well

Thanks again

Last edited by lxdorney; 07-15-2015 at 03:01 AM..
# 5  
Old 07-15-2015
Hello lxdorney,

Following is the explanation for same, hope that helps you. Also please always do show us your efforts as we all are here to learn. You can hit thanks button present at bottom of threads to anyone to whom you want to thank or appreciate.
Code:
awk '{if($0 ~ /^ \[A\.B/){gsub(/\[|\]/,X,$0);sub(/\,/,"<br>",$0)};print;next} 1'  Input_file
 
if($0 ~ /^ \[A\.B/)            #### looking for condition where it matches line which is starting from [A.B
gsub(/\[|\]/,X,$0)             #### If above if condition is TRUE then substitute all matches of [ and ] characters in that matched line with NULL.
;sub(/\,/,"<br>",$0)           #### Then substitute first match of charcter , wth <br>
;print                         #### Do print operation to newly formed line includng changes in above steps
next                           #### Now don't do any further operaion and go to next line
1                              #### awk works on condition and operation method so when condition is TRUE do certain operations as mentioned by user.
                                    so by giving 1 we are making condition TRUE and then no action/operatin given by me to it will perform default action/operation
                                    which is printing the lines.

Hope above helps you. Enjoy learning.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 07-15-2015
In current context, you can simplify like this also

Code:
[akshay@localhost tmp]$ cat file
 [A.B. No. T-8346, January 01, 2015 ]
JHON VS. PETER, AGOO PET.

How Old Are You
 [A.C. No. T-8346, January 01, 2015 ]
JHON VS. PETER, AGOO PET.

How Old Are You

If pattern found, then substitute first found comma with <br> and global substitution, +1 default action print line.
Code:
[akshay@localhost tmp]$ awk '(/^ \[A.B/ && sub(/,/,"<br>") && gsub(/[\[\]]/,""))+1' file

Code:
 A.B. No. T-8346<br> January 01, 2015 
JHON VS. PETER, AGOO PET.

How Old Are You
 [A.C. No. T-8346, January 01, 2015 ]
JHON VS. PETER, AGOO PET.

How Old Are You

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 07-15-2015
Quote:
Originally Posted by Akshay Hegde
... ... ...
Code:
[akshay@localhost tmp]$ awk '(/^ \[A.B/ && sub(/,/,"<br>") && gsub(/[\[\]]/,""))+1' file

... ... ...
Although the above works with some versions of awk, the standards say the only backslash escapes that are required to be recognized by awk are:\\, \/, \", \a, \b, \f, \n, \r, \t, and \v, and \d, \dd, and \ddd (where d is an octal digit). Anything else following a \ in a bracket expression produces undefined behavior.

In a bracket expression, if the first character inside the brackets (or immediately after the ^ in a non-matching list in a non-matching bracket expression) is a closing bracket, that closing bracket is a character in the list; not a terminator for the matching expression. And, inside a bracket expression, there is nothing special about an opening bracket except when it appears as the start of a collating symbol expression, the start of an equivalence class expression, or the start of a character class expression. So, two standard ways to specify an ERE containing a bracket expression matching the two characters [ and ] in awk are /[][]/ and "[][]".
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract the whole set if a pattern matches

Hi, I have to extract the whole set if a pattern matches.i have a file called input.txt input.txt ------------ CREATE TABLE ABC ( A, B, C ); CREATE TABLE XYZ ( X, Y, Z, P, Q ); (6 Replies)
Discussion started by: raju2016
6 Replies

2. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

3. Shell Programming and Scripting

Insert certain field of matched pattern line above pattern

Hello every, I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|" eg > # @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp Pleistocene"|Q # @P... (5 Replies)
Discussion started by: jyu3
5 Replies

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

5. Shell Programming and Scripting

How Not to Delete Words that matches a PATTERN

Hi, I have a test file name test.txt with its contents string 21345 qwee strinn strriin striin i need to delete all the words except the word STRING I used the command cat test.txt | sed 's/^..*$/**/g' but the output entries still contain strinn strriin striin. Plz Help me out.... (5 Replies)
Discussion started by: Ananth12
5 Replies

6. Shell Programming and Scripting

Remove if the above line matches pattern

but keep if does not I have a file: --> my.out foo: bar foo: moo blarg i am on vacation foo: goose foo: lucy foo: moose foo: stucky groover@monkey.org foo: bozo grimace@gonzo.net dear sir - blargo blargo foo: goon foo: sloppy foo: saudi gimme gimme gimme (3 Replies)
Discussion started by: spacegoose
3 Replies

7. Shell Programming and Scripting

get value that matches file name pattern

Hi I have files with names that contain the date in several formats as, YYYYMMDD, DD-MM-YY,DD.MM.YY or similar combinations. I know if a file fits in one pattern or other, but i donīt know how to extract the substring contained in the file that matches the pattern. For example, i know that ... (1 Reply)
Discussion started by: pjrm
1 Replies

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

9. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies

10. Shell Programming and Scripting

Extract if pattern matches

Hi All, I have an input below. I tried to use the awk below but it seems that it ;s not working. Can anybody help ? My concept here is to find the 2nd field of the last occurrence of such pattern " ** XXX ccc ccc cc cc ccc 2007 " . In this case, the 2nd field is " XXX ". With this "XXX" term... (20 Replies)
Discussion started by: Raynon
20 Replies
Login or Register to Ask a Question