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?
# 8  
Old 06-26-2013
Quote:
Originally Posted by radoulov
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).$3.("Y" x 40).$5/e;
  print ' abcdererdfd1233345r3434sdswewe
abXXXXXXXXXXXXXXXXXXXXreYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYd1233345r3434sdswewe

the first script look work, but How I can let it read the file. right now, it only takes the line in the script.
# 9  
Old 06-26-2013
Code:
awk 'BEGIN {
  FIELDWIDTHS = "2 3 2 3 99"
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }
  }
{  
  print $1 x $3 y $5             
  }' infile

Or:
Code:
perl -pe's/(..)...(..)...(.*)/$1.("X" x 20).$2.("Y" x 40).$3/e' infile

Or:
Code:
awk 'BEGIN {
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }  
  }    
{
  print substr($0, 1, 2) x substr($0, 6, 2) y substr($0, 11)    
  }' infile

This User Gave Thanks to radoulov For This Post:
# 10  
Old 06-26-2013
Quote:
Originally Posted by radoulov
Code:
awk 'BEGIN {
  FIELDWIDTHS = "2 3 2 3 99"
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }
  }
{  
  print $1 x $3 y $5             
  }' infile

Or:
Code:
perl -pe's/(..)...(..)...(.*)/$1.("X" x 20).$2.("Y" x 40).$3/e' infile

Or:
Code:
awk 'BEGIN {
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }  
  }    
{
  print substr($0, 1, 2) x substr($0, 6, 2) y substr($0, 11)    
  }' infile

Thanks, it works.

I have another posting at
https://www.unix.com/shell-programmin...ther-file.html
do you have any idea about this. actually, these two for same thing, because I have 80 columns and 37 columns need to be replaced, plus I have different files need to be done with same way, so I thought maybe I can create instruction file and using common command for all the files. if you think this idea doesn't make scens please let me know. I am a DB developer and not that good in linux. Hopefully I can get solution from you guys.
Thanks again.

---------- Post updated at 05:27 PM ---------- Previous update was at 04:56 PM ----------

Quote:
Originally Posted by radoulov
Code:
awk 'BEGIN {
  FIELDWIDTHS = "2 3 2 3 99"
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }
  }
{  
  print $1 x $3 y $5             
  }' infile

Or:
Code:
perl -pe's/(..)...(..)...(.*)/$1.("X" x 20).$2.("Y" x 40).$3/e' infile

Or:
Code:
awk 'BEGIN {
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }  
  }    
{
  print substr($0, 1, 2) x substr($0, 6, 2) y substr($0, 11)    
  }' infile

these codes work fine.
for the first set of code,
Code:
awk 'BEGIN {
  FIELDWIDTHS = "2 3 2 3 99"
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }
  }
{  
  print $1 x $3 y $5             
  }' infile

I want to put "2 3 2 3 99" in separate file, this way I can use this for different case.
My question is how could I change the current code to make it work.
I tried as below code, but it doesn't pick up the variable value.
Code:
awk -v fw="$(cat seed)" 'BEGIN {
  FIELDWIDTHS = fw
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "x"
    y = y "y"
    }
  }
{
  print $1 x $3 y $5
  }' infile

Thanks in advance
# 11  
Old 06-27-2013
Code:
awk  'BEGIN {
  getline FIELDWIDTHS < "seed"
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }
  }
{
  print $1 x $3 y $5
  }' infile

This User Gave Thanks to radoulov For This Post:
# 12  
Old 06-27-2013
Quote:
Originally Posted by radoulov
Code:
awk  'BEGIN {
  getline FIELDWIDTHS < "seed"
  cx = 20; cy = 40
  for (i = 0; ++i <= cy;) {
    i <= cx && x = x "X"
    y = y "Y"
    }
  }
{
  print $1 x $3 y $5
  }' infile

Thanks for your reply.
when running the script I got error:
Code:
awk: cmd. line:1: fatal: invalid FIELDWIDTHS value, near `2,4,6,10,99'

I tried file format:
Code:
"2,4,6,10,99" and 2,4,6,10,99

# 13  
Old 06-27-2013
The FIELDWIDTHS must be separated by space, not by comma:
Code:
2 4 6 10 99

not:
Code:
2,4,6,10,99

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