How to replace all but the first 3 characters with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace all but the first 3 characters with sed?
# 22  
Old 02-12-2015
Hi,
for fun in bash:
Code:
time { F(){ set -- ${2:3} ${2:0:3} ; echo "$2${1//?/*}" ;} ; mapfile -c 1 -C F <file1 ;}
aba*****
cdc*******
efe******
a*b****

real	0m0.001s
user	0m0.003s
sys	0m0.000s

I'm surprise of execution time...
Regards.
# 23  
Old 02-12-2015
The execution time can only be used relative to other solutions on the same system, so it cannot be used as an absolute measure. Also, the size of the input file should be sufficiently large that, so that the measurement differences are significant. I used a file with many lines and characters for example, not the the sample input in this thread....
# 24  
Old 02-12-2015
Ok, I understand better, so here:

Time reference:
Code:
time awk '{x=substr($0,N+1); gsub(".","*",x); print substr($0,1,N) x}' N=3 file1 > /dev/null
real	0m0.038s
user	0m0.037s
sys	0m0.000s

The better such as wisecracker:
Code:
time { while read ; do X="${REPLY:3}" ; echo "${REPLY:0:3}${X//?/*}" ;done <file1 >/dev/null ;}
real	0m0.177s
user	0m0.163s
sys	0m0.013s

My part (for fun because use a lot of memory and slower previous):
Code:
time { F(){ set -- "${2:3}" "${2:0:3}" ; echo "$2${1//?/*}" ;} ; mapfile -c 1 -C F <file1 >/dev/null ;} 
real	0m0.286s
user	0m0.280s
sys	0m0.000s

Of sea (really slow):
Code:
time { while read line; do len=${#line} ; [ $len -gt 3 ] && len=$(( $len - 3)); tmp="$(printf '%*s' $len|sed s,\ ,*,g)"; echo "${line:0:3}${tmp}"; done<file1 >/dev/null;}
real	0m27.940s
user	0m12.867s
sys	0m1.340s

And a perl version seems faster that awk:
Code:
time perl -ne 'print substr($_,0,3)."*"x(length()-4)."\n"' file1 >/dev/null 
real	0m0.014s
user	0m0.010s
sys	0m0.000s

Regards.

Last edited by disedorgue; 02-12-2015 at 07:14 PM.. Reason: Replace in perl version length()-3 by length()-4
# 25  
Old 02-13-2015
Try C:
Code:
#include <stdio.h>
#include <string.h>
 
int main(){
    char buf[256];
    size_t sz ;
 
    while ( fgets( buf, sizeof buf, stdin )){
        if ( ( sz = strlen( buf )) < 5 ){
            fputs( buf, stdout );
        } else if ( sz == ( sizeof buf - 1 ) && buf[254] != '\n' ){
            fputs( "Line too long!\n", stderr );
            exit( 1 );
        } else {
            fwrite( buf, 3, 1, stdout );
            for ( sz -= 4 ; sz ; sz-- ){
                putchar( '*' );
            }
            putchar( '\n' );
        }
    }
 
    if ( ferror( stdin )){
        perror( "stdin" );
        exit( 2 );
    }
}

# 26  
Old 02-13-2015
Just for information, your code C give:
Code:
time ./a.out <file1 >/dev/null 

real	0m0.006s
user	0m0.003s
sys	0m0.000s

But there is cheated, it is no longer scripting Smilie

Regards.
# 27  
Old 02-13-2015
Try JAVA, then:
Code:
import java.io.BufferedReader; 
import java.io.InputStreamReader;
public class star3 {
public static void main( String[] args ) {
String buf ; BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ));
int sz ;
try { while (( buf = stdin.readLine()) != null ){ if (( sz = buf.length()) < 4 ){
System.out.println( buf ); } else { System.out.print( buf.substring( 0, 3 ));
for ( sz -= 3 ; sz > 0 ; sz-- ){
System.out.print( '*' );
}
System.out.println(); } }
} catch( Exception e ){ e.printStackTrace(); System.exit( 1 ); }
}
}

Not sure why website reformats JAVA!
# 28  
Old 02-13-2015
Not terrible (java 1.7.0_55):
Code:
time java star3 <file1 >/dev/null 

real	0m0.431s
user	0m0.527s
sys	0m0.073s

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command on AIX, replace specific characters

Hi, Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed. the source data that I have is$cat v.txt 4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY... (9 Replies)
Discussion started by: dsid
9 Replies

2. Shell Programming and Scripting

sed replace characters using a wildcard

Hello, I have some data that looks like the following, > <SALTDATA> (OVS0199262) HCl > <IDNUMBER> (OVS0199262) OVS0199262 > <SUPPLIER> (OVS0199262) TimTec > <EMAIL> (OVS0199262) info@timtec.net > <WEBSITE> (OVS0199262) http://www.timtec.net I need to remove the data in... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

sed replace nth characters with string

Hi, I hope you can help me out please? I need to replace from character 8-16 with AAAAAAAA and the rest should stay the same after character 16 gtwrhtrd11111111rjytwyejtyjejetjyetgeaEHT wrehrhw22222222hytekutkyukrylryilruilrGEQTH hrwjyety33333333gtrhwrjrgkreglqeriugn;RUGNEURGU ... (4 Replies)
Discussion started by: stinkefisch
4 Replies

4. Shell Programming and Scripting

Replace characters infile with sed

I have several files in a directory that look like this: jacket-n r potential-n - outcome-n f reputation-n b I want to replace the characters in the second column with certain numbers. For instance, I want the letters 'f', 'r' and 'b' in the second column to replaced with 0 and I want the... (1 Reply)
Discussion started by: owwow14
1 Replies

5. Shell Programming and Scripting

sed replace range of characters in each line

Hi, I'm trying to replace a range of characters by their position in each line by spaces. I need to replace characters 95 to 145 by spaces in each line. i tried below but it doesn't work sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt can someone please help me... (3 Replies)
Discussion started by: Kevin Tivoli
3 Replies

6. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

7. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

8. Shell Programming and Scripting

Using sed to replace special characters

Hi everyone I have file1 contains: '7832' ' 8765 6543 I want a sed command that will format as: '7832' , '8765' , '6543' I tried sed -e s/\'//g -e 's/^*//;s/*$//' file1 > file2 sed -e :a -e '$!N; s/\n/ /; ta' file2 which gives: 7832 8765 6543 I need some help to continue with... (5 Replies)
Discussion started by: nimo
5 Replies

9. Shell Programming and Scripting

Sed replace characters not equal to an expression

Hi all, Suppose I have a file with the contents below, and I only want to print words %S_ then | sort -u. ------------------------------ The %S_MSG that starts with '%.*s' is too long. Maximum length is %d. The %S_MSG name '%.*s' contains more than the maximum number of prefixes. The... (5 Replies)
Discussion started by: poldo
5 Replies

10. Shell Programming and Scripting

how to replace control characters using sed?

How can I use sed to replace a ctrl character such as 'new line' (\0a) to something else? Or any other good command can do this job? Thanks, Hillxy (5 Replies)
Discussion started by: hillxy
5 Replies
Login or Register to Ask a Question