Replace the line with specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace the line with specific pattern
# 1  
Old 04-09-2014
Replace the line with specific pattern

Hello All

I'm trying to change one string from a file contening this patern:
xxxx-xxxx

4 numbers - end 4 other numbers
This is a sample of the file:

Code:
LDR  00679 am a2200205   4500
=001  3617
=008  030219s2000\\\\xxx|||||\||||\00|\0\spa\d
=020  \\$a0211-1942
=041  \\$aCastellà
=093  \\$a19/02/2003
=095  \\$a12/11/2001
=100  \\$aGorrotxategi Anieto, Xabier$91930
=245  \\$aArte paleolítico parietal de Bizcaia


as you see there is a line :=020 \\$a0211-1942
So I would like to detect deep in a big file with a lot of records like this. and every row with xxxx-xxxx replace the code =020 by =022

I know that sed can do this but I have no idea how... Some one can help ??

Thanks a lot

Last edited by vbe; 04-09-2014 at 05:37 AM.. Reason: code tags not icode please
# 2  
Old 04-09-2014
Code:
 sed -i.bak 's:=020 \(\\\\$a[0-9]\{4\}-[0-9]\{4\}\):=022 \1:' file

# 3  
Old 04-09-2014
Hi
thanks for this rapid answer... Whooaw
So I guess that the way to grab the xxxx-xxxx patern is:

(\\\\$a[0-9]\{4\}-[0-9]\{4\}\)

The start is parentesis write?
I do not anderstand the \\\\ before $a
Then I anderstand this:
[0-9]\{4\}-[0-9]\{4\
it means.. get numeric numbers length 4... isn't it?

Thanks again

---------- Post updated at 03:50 AM ---------- Previous update was at 03:43 AM ----------

Hello again

and...if I want to check de print before replace?
May this sintaxe ok?
sed -p'\(\\\\$a[0-9]\{4\}-[0-9]\{4\}\) \1:' file


Thanks
# 4  
Old 04-09-2014
Hello,

Following may help you in same.


Code:
awk '{match($0,/.*[0-9][0-9][0-9][0-9]\-[0-9][0-9][0-9][0-9]/); a=substr($0,RSTART+4,RLENGTH); {if(a){print "022" a}}}' check_data_check_rem_check12113

Output will be as follows.

Code:
022 \\$a0211-1942

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 04-09-2014
Hi

very usefull. Thank you very much !!
Does any one can paste url of a tutorial sed and awk for beginners??
jajaja This tools seem to be very powerfull.

Cheers
# 6  
Old 04-09-2014
Quote:
Originally Posted by ldiaz2106
Hi

very usefull. Thank you very much !!
Does any one can paste url of a tutorial sed and awk for beginners??
jajaja This tools seem to be very powerfull.

Cheers
I advice for this links for you
Famous Sed One-Liners Explained, Part I: File Spacing, Numbering and Text Conversion and Substitution - good coders code, great reuse
Sed - An Introduction and Tutorial

Regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

2. Shell Programming and Scripting

Replace specific characters until pattern

Hi experts, My file looks something like this. abcXX4,7,234 abc,defg,45XX23,74,123 The number of commas left of the XX can vary. The question is how can I replace all the commas left of the 'XX' with an underscore? abcXX4,7,234 abc_defg_45XX23,74,123 Thanks! (5 Replies)
Discussion started by: abercrom
5 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Shell Programming and Scripting

Need help to replace a pattern on specific line in a data file

Hi, I want to replace specific pattern "-2.0000 2" by "1.0000 3" on a particular line (line #5) in a file 1.dat. I have about 50 more files similar to 1.dat in which I want to do this correction. Can you please suggest how can I make change only at this particular line of a file... (6 Replies)
Discussion started by: anuj06
6 Replies

5. Shell Programming and Scripting

Help with replace the content of specific pattern

Input file: __<name>AWEETET</name> ____<name_evidence="3"_type="2@#">QEWQE</name> __<name>QWE048</name> ____<name_evidence="3"_type="570">@#@$#545</name> ____<name_evidence="2"_type="351">QWE4</name> Desired output: __<tmp>AWEETET</tmp> ____<name_evidence="3"_type="2@#">QEWQE</name>... (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

7. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Shell Programming and Scripting

pattern match and replace another pattern in same line

I have a pattern username:x:32005:32006::/usr/local/user:/bin/bash I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell So it becomes username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Discussion started by: anilcliff
7 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

serach and replace a specific pattern or value in a xml file

can some one help me with a perl command i have to search and replace a version from a xml-file so i use in a ksh script a command like this ssh $GLB_ACC@$GLB_HOST "/usr/contrib/bin/perl -pi -e "s/$curVersion/$new_Version/g" $Dest_dir/epi.xml" this command worked so far, but the problem... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies
Login or Register to Ask a Question