How to use regular exp to replace the character in the file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use regular exp to replace the character in the file?
# 1  
Old 06-26-2013
How to use regular exp to replace the character in the file?

Hi Unix Gurus,

yesterday I asked a question and got answer, it works fine.
I have one more thing need to help
in the code
Code:
awk '{print substr($0,1,3)"xxx"substr($0,7)}' file

If I have 50 charactor's need to be replaced, is there any easy way to use reg exp or I have to input 50 XXXXx......

Thanks in advance
# 2  
Old 06-26-2013
You could do something like this:

Code:
awk -v x=$(printf 'x%.s' {1..50}) '{print substr($0,1,3) x substr($0,7)}' f

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 06-26-2013
You can do it all in awk:

Code:
awk 'BEGIN { 
  c = "x"; n = 50
  for (i = 0; ++i <= n;) s = s c
  }
{
  print substr($0, 1, 3) s substr($0, 7)
  }' infile

This User Gave Thanks to radoulov For This Post:
# 4  
Old 06-26-2013
Quote:
Originally Posted by radoulov
You can do it all in awk:

Code:
awk 'BEGIN { 
  c = "x"; n = 50
  for (i = 0; ++i <= n;) s = s c
  }
{
  print substr($0, 1, 3) s substr($0, 7)
  }' infile

The script works, would you please explain a little bit about "s = s c", what this is doing?

Thanks

---------- Post updated at 12:42 PM ---------- Previous update was at 12:08 PM ----------

Quote:
Originally Posted by Subbeh
You could do something like this:

Code:
awk -v x=$(printf 'x%.s' {1..50}) '{print substr($0,1,3) x substr($0,7)}' f

Thanks for your reply. the script works.
one more question, If I have two parts of string need to be replaced. how could I achieve this?
for example:
I have original string like
Code:
abcdererdfd1233345r3434sdswewe

I need replace from 3rd to 5th with 20 'X', from 8th 10th with 40 'Y'
Code:
ab"XXXX... 20X"re"YYYY.... 40Y"d1233345r3434sdswewe

thanks in advance

---------- Post updated at 12:49 PM ---------- Previous update was at 12:42 PM ----------

Quote:
Originally Posted by radoulov
You can do it all in awk:

Code:
awk 'BEGIN { 
  c = "x"; n = 50
  for (i = 0; ++i <= n;) s = s c
  }
{
  print substr($0, 1, 3) s substr($0, 7)
  }' infile

one more question, If I have two parts of string need to be replaced. how could I achieve this?
for example:
I have original string like

Code:
Code:
abcdererdfd1233345r3434sdswewe

I need replace from 3rd to 5th with 20 'X', from 8th 10th with 40 'Y'

Code:
Code:
ab"XXXX... 20X"re"YYYY.... 40Y"d1233345r3434sdswewe

thanks in advance
# 5  
Old 06-26-2013
Something like this:

Code:
awk 'BEGIN {
  s = ARGV[1]
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "x"
    y = y "y"
    }  
  print substr(s, 1, 2) x substr(s, 6, 2) y substr(s, 11)    
  }' <your_string>

Code:
% awk 'BEGIN {
  s = ARGV[1]
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "x"
y = y "y"
}
  print substr(s, 1, 2) x substr(s, 6, 2) y substr(s, 11)
  }' abcdererdfd1233345r3434sdswewe
abxxxxxxxxxxxxxxxxxxxxreyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyd1233345r3434sdswewe

# 6  
Old 06-26-2013
Try also
Code:
awk 'BEGIN {X=sprintf ("%50s"," "); gsub (" ","x", X); print X}'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You can do the same for Y...
# 7  
Old 06-26-2013
Another one with GNU awk:

Code:
% awk 'BEGIN {
  FIELDWIDTHS = "2 3 2 3 99"
  $0 = ARGV[1]
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "x"
y = y "y"
}
  print $1 x $3 y $5             
  }'   abcdererdfd1233345r3434sdswewe
abxxxxxxxxxxxxxxxxxxxxreyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyd1233345r3434sdswewe

And Perl (I've just realized, that you want X and Y, not x and y Smilie):

Code:
perl -le '($_ = shift) =~ 
  s/(..)...(..)...(.*)/$1.("X" x 20).$2.("Y" x 40).$3/e;
  print ' abcdererdfd1233345r3434sdswewe
abXXXXXXXXXXXXXXXXXXXXreYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYd1233345r3434sdswewe


Last edited by radoulov; 06-26-2013 at 05:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace character in a file.

I have a data like this in a file. 05/08/2017,U,01,116326001 05/08/2017,U,01,116226001 05/08/2017,U,01,116726323 05/08/2017,U,01,116236001 I want replace the date(1st column) of all records. Ex: 05/08/2017 to 04/02/2017 Please use CODE tags when displaying sample input, sample output,... (1 Reply)
Discussion started by: Artlk
1 Replies

2. Shell Programming and Scripting

Bash script reg-exp , replace , open and write

Hi All I am a new in scripting language and I would like help for you guys I would like to create a file named constant.h and search into all files *.m in specific directory for a reg-exp @"LBL_]+" exp: @"LBL_75847" , and write those matchs to constant.h if there are not written (no... (15 Replies)
Discussion started by: molwiko
15 Replies

3. Shell Programming and Scripting

Replace a character in a file

Hello, Can anyone help me? I need to replace the letters (A, B, C..) of the second column in this file by a number i.e. 1. current file 0123456 0A123 0003355 Marion 1234567 0B117 0003388 Anthony 1112333 0C345 0459877 Ann new file 0123456 01123 0003355 Marion 1234567 ... (3 Replies)
Discussion started by: Lilu_CK
3 Replies

4. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

5. Shell Programming and Scripting

Regular exp in awk

Hi all, One small doubt, reg exp in awk, is it possible to extract the match of regular expression like in perl? eg: B R16288 Map3_NoCat B R16067 Map4_NoCat B R16647 Map3_NoCat B R16450 Map3_NoCat B R16106 Map6_NoCat B R16000 Map3_NoCat B R16395 Map3_NoCat B R16243 Map3_NoCat B R16023... (6 Replies)
Discussion started by: gvj
6 Replies

6. Shell Programming and Scripting

Replace last character in file

Greetings UNIX folks, I am running a tcsh script and trying to replace the last character of a file with the number 0. I know that the last character of the file will always be 1, but I only want to replace the last character. Example: 78062 26.55 78.77 1 MYGF1 24.89 15.78 1 ... (2 Replies)
Discussion started by: TheSMan5
2 Replies

7. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

8. UNIX for Dummies Questions & Answers

extract a part of a path like (input: /etc/exp/home/bin ====> output: exp)

Hi, I need to make some extraction . with the following input to get the right output. input: /etc/exp/home/bin ====> output: exp and input: aex1234 ===> output: ex Thanks for your help, (4 Replies)
Discussion started by: yeclota
4 Replies

9. Shell Programming and Scripting

How to replace a character in the file?

Hi All, I have file contains the following information. chem00s4.mis.amat.com ] Critical 3/21 chem00s4.mis.amat.com ] Normal 3/22 chem00s4.mis.amat.com ] Normal 3/23 chem00s4.mis.amat.com ] Normal 3/24 chem00s4.mis.amat.com ] Critical 3/25... (2 Replies)
Discussion started by: ntgobinath
2 Replies

10. Shell Programming and Scripting

How to replace a character in a file

Hi, I want to replace a character in every line in the file say the file looks like this SOBO20060830094122140014541834 WENP0414541835 ] SOBO20060830094121140014541834 WENP0414541835 SOBO20060830094121140014541834 WENP0414541835 I want to replace the blue 00 by TS. (6 Replies)
Discussion started by: preethgideon
6 Replies
Login or Register to Ask a Question