0

I am using a while loop to read a file line by line and, for example, if the file contains a line like:

ABC.///.AB_SWift_ABC

I need to replace it with

ABC.slashslashslash.AB_SWift_ABC

How can I do that with a Korn shell?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pravin_23
  • 128
  • 2
  • 2
  • 11

3 Answers3

0

If you are using bash, you can use the variable substring replacement functionality.

>> a=ABC.///.AB_SWift_ABC
>> echo $a
ABC.///.AB_SWift_ABC
>> echo ${a//\//slash}
ABC.slashslashslash.AB_SWift_ABC
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

Why are you reading line by line? It would be quicker to simply run sed on the file:

sed 's%/%slash%g'

If you must do it in Bash, then use the substitute shell parameter expansion:

${line//\//slash}

You mention backslashes but use forward slashes, which is confusing. If, perchance, you mean backslashes, then:

sed 's/\\/slash/g'
${lines//\\/slash}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • i am reading a file line by line and doing some process in it. after the processing i am trying to create a new file . example : if the line is like ABC.ABC_swift_Q i am amble to create a file . if the line consists of names like this ABC.///.AB_SWift_ABC there exists a problem ... some process > home/dir/ABC.ABC_swift_Q --here file gets created if some process > home/dir/ABC.///.AB_SWift_ABC --here it takes ABC as a directory so i am trying to replace / with SLASH – pravin_23 Apr 02 '14 at 07:11
  • OK; then if you've read the line (file name) into `$line` (with `read line` or minor variants on the theme), you can write `mapped=${line//\//slash}` and then use `"$mapped"` where you need the name without slashes and `"$line"` where you need the name with slashes. – Jonathan Leffler Apr 02 '14 at 07:16
  • When i try doing it in Kshell ,it gives me bad substituion error – pravin_23 Apr 02 '14 at 09:10
  • Yes; Korn shell is not Bash. AFAIK, you'll need to use a `sed`-based solution in Korn shell: `mapped=$(echo "$line" | sed 's%/%slash%g')`. – Jonathan Leffler Apr 02 '14 at 10:22
0

In c you can replace / by slash using bash command like

char cmd[]="sed -i 's|/|slash|g' file";
system(cmd);

So not require read file line by line.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73