search & replace pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search & replace pattern
# 8  
Old 10-18-2011
Quote:
Originally Posted by dips_ag
Hi Vgersh,

Thanks for the solution Smilie!!

Can you please explain this sed command ?

-dips
Code:
sed 's#YYYY[^_.]*#*#'

Anything that starts with 'YYYY' followed by anything, BUT '_' or '.' is to be replaced with a '*'.
# 9  
Old 10-19-2011
Hi Vgersh,

Quote:
Originally Posted by vgersh99
I assumed the 'YYYYMMDD' was actually a mnemonic for the date/time numeric spec.
If you want to take 'YYYYMMDD' literally - that's even easier...:
Code:
sed 's#YYYY[^_.]*#*#' myFile

I think I have an additional requirement involving extraction of YYYYMMDD parttern, however what you assumed about this being a mnemonic for data/time data is now true. Can you help me out again?

Code:
 
For e.g.
i/p1: 45_*_20111019_SRC.txt.tar.gz -> o/p: 20111019
i/p2: 201110192359_45_*_SRC.txt.tar.gz -> o/p: 201110192359
i/p3: 45_*_SRC_2011101923.txt.tar.gz -> o/p: 2011101923

I attempted the below code:
Code:
echo 45_*_20111019_SRC.txt.tar.gz | sed 's/.....\(.\{8\}\)\(.*\)/\1/'

but I know this is in vain because it's too much tied up with the length of the string & the pattern length!!
-dips
# 10  
Old 10-19-2011
Quote:
Originally Posted by dips_ag
Hi Vgersh,



I think I have an additional requirement involving extraction of YYYYMMDD parttern, however what you assumed about this being a mnemonic for data/time data is now true. Can you help me out again?

Code:
 
For e.g.
i/p1: 45_*_20111019_SRC.txt.tar.gz -> o/p: 20111019
i/p2: 201110192359_45_*_SRC.txt.tar.gz -> o/p: 201110192359
i/p3: 45_*_SRC_2011101923.txt.tar.gz -> o/p: 2011101923

I attempted the below code:
Code:
echo 45_*_20111019_SRC.txt.tar.gz | sed 's/.....\(.\{8\}\)\(.*\)/\1/'

but I know this is in vain because it's too much tied up with the length of the string & the pattern length!!
-dips
you have to assume a minimum length of numbers to determine if it's a date or not. As your dates may differ in length, I assumed the minimum length of 8 -\{8,\}. Notice the trailing comma in the spec. From man ed:
Code:
       *    An RE followed by:
              \{m\}
                   Matches exactly m occurrences of the character matched by
                   the RE.
              \{m,\}
                   Matches at least m occurrences of the character matched by
                   the RE.
              \{m,n\}
                   Matches any number of occurrences of the character matched
                   by the RE from m to n inclusive.

considering file dip.txt:
Code:
45_*_20111019_SRC.txt.tar.gz
201110192359_45_*_SRC.txt.tar.gz
45_*_SRC_2011101923.txt.tar.gz

the following:
Code:
sed 's#^.*_\([0-9]\{8,\}\)[_.]*.*#\1#;s#_.*##' dip.txt

produces:
Code:
20111019
201110192359
2011101923

This User Gave Thanks to vgersh99 For This Post:
# 11  
Old 10-19-2011
Hi Vgersh,

Simply Superb !!Smilie Thanks again.

-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search & Replace

Hi Gurus, I have two files. I want to read sessoin_name from the file1 and replace $Param4 & $Param5 in file2 with connection_name in specified in file1. The file1 will have data in following format File 1 session_name,connection_name s_abcd,Listener_2 s_def,Listener_1 source file... (7 Replies)
Discussion started by: r_t_1601
7 Replies

2. Shell Programming and Scripting

Search & Replace

Hi all Please can you help me with a script to check several files for the following string: encoding=""and replace it with: encoding="UTF-8"I did the following, : #!/bin/sh string1="encoding=""" string2="encoding="UTF-8" sed 's/'"$string1"'/'"$string2"'/g'but does not work. Please can... (18 Replies)
Discussion started by: fretagi
18 Replies

3. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

4. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

5. UNIX for Dummies Questions & Answers

Search & Replace

Hi , I ahve a text file which has several instances of the text such as run_time: 09:30 I need to add double quotes before and after the time value i.e: run_time: "09:30" Any suggestions on how to go about the same (4 Replies)
Discussion started by: jobbyjoseph
4 Replies

6. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

7. Shell Programming and Scripting

Search for a Pattern and Replace

Hello All, Can you help me with this , I need to search a pattern replace it with the new pattern in all the files in a directory. what would be the easiest way to do that? Thanks in advance. :) Sam, (6 Replies)
Discussion started by: sbasetty
6 Replies

8. Shell Programming and Scripting

Need help with search & replace

I have a file that has some accent characters in it when viewed in some text editors, but when viewed in vi they come in as ~R and ~U. I need to make a script to remove these characters from the file, but have been unsuccessful. I am not sure how sed or awk, or something similar is viewing them,... (8 Replies)
Discussion started by: tcovert
8 Replies

9. Shell Programming and Scripting

Search & replace

Is there any way we can achieve search & replace with awk? I could achieve the same with sed in following way - sed 's/A/B/g' file1 > file2 But the same regex if I try with using awk following way, awk 's/A/B/g' file1 > file2 it gives me Syntax error. I strongly believe I am... (1 Reply)
Discussion started by: videsh77
1 Replies

10. Shell Programming and Scripting

Help, sed search&replace

Plzzzz, tell me some script about this... What does this mean ? sed '/^ */s///' sed '/^/s// /' and why it's diferent ??? sed '/ */s// /g' and sed 's/ */ /g'. It's all the same ??? Thanks you very much (2 Replies)
Discussion started by: mle
2 Replies
Login or Register to Ask a Question