Home > Mobile >  Shell script to replace alphabets to 'X' if line starts with a pattern
Shell script to replace alphabets to 'X' if line starts with a pattern

Time:09-23

I have a file under a folder in Unix box. My file will be looking like below, where ever the line starts with 'LIN' , 'RFF', 'NAD' those lines alphabets and numbers to be masked to 'X' and '0' respectively, excluding 'LIN' , 'RFF', 'NAD'.

I've tried with below command so far like each alphabet one command, want to make the script that will mask those lines in a single run .

$ sed '/^LIN/s/A/X/'

Sample File :-

#Header#  
line1:string::strings:  
line2:asdasd:asd:aD:  
line3:asda:asda  
....  
....  
LIN:asdas:SDFGH:1223:asf  
....  
NAD:asdas123:23:  
....
....  
RFF:asda:asd123:asd  
....  
Nlines:asda:asdad:asdas  
#Footer#



CodePudding user response:

You could combine 2 sed commands into one using the semi-colon. On lines that start with LIN or NAD or RFF, search for alpha characters and replace with X but start at the 4th character, preserving the leading characters. Then, search for digits and replace with 0's.

$ sed '/^\(LIN\|NAD\|RFF\):/s/[[:alpha:]]/X/4g;s/[[:digit:]]/0/g' file.txt
#Header#
line0:string::strings:
line0:asdasd:asd:aD:
line0:asda:asda
....
....
LIN:XXXXX:XXXXX:0000:XXX
....
NAD:XXXXX000:00:
....
....
RFF:XXXX:XXX000:XXX
....
Nlines:asda:asdad:asdas
#Footer#
$

CodePudding user response:

Thanks much for the help , i have tested it with .txt file but still few strings in upper and lower are not masking with X, pls suggest , i am also trying on top of your command .

  • Related