Solved Replacing specific pipes in file using vi

June 14, 2011 at 00:32:11
Specs: Windows XP
I have a text file that is pipe delimited:

314.94|416190|001|1.0|

I need to replace each pipe with different strings e.g:

first pipe must be replaced with:

\" and nappi = \"


See More: Replacing specific pipes in file using vi

Report •

✔ Best Answer
June 17, 2011 at 07:15:05
First, you're welcome. Glad to help.

Next, there are numerous ways to do this; this stub prints HELLO in front of every line of file: data.txt

awk ' { print "HELLO "$0 } ' data.txt

This is my favorite awk tutorial:

http://www.grymoire.com/Unix/Awk.html



#1
June 14, 2011 at 12:13:22
On Solaris 9, within vi - command mode - press the colon key and type:

g/|/s//\\" and nappi = \\"/

If the one line above is in the file, the above command prints:

314.94\" and nappi = \"416190|001|1.0|


Report •

#2
June 14, 2011 at 23:26:46
Thank you but this will replace all of the "|" with "xxx" e.g:

314.94|416190|001|1.0|

:g/|/s//xxx/g

result in:

314.94xxx416190xxx001xxx1.0xxx

I want for example the first "|" replaced by "xxx" second "|" replaced by "yyy" etc... e.g:

314.94xxx416190yyy001zzz1.0|


Report •

#3
June 15, 2011 at 07:19:27
In vi, you are not going to be able to do this with one substitution command. Obviously, you can execute multiple substitution commands and each pipe symbol is changed in turn:

:g/|/s//xxx/
:g/|/s//yyy/
:g/|/s//zzz/

Otherwise, I think you need a shell script. Something like this will do it, but you have to place the changes in the array in the BEGIN section nosubs:

#!/bin/ksh

awk ' BEGIN {
myarr[1]="xxx"
myarr[2]="yyy"
myarr[3]="zzz"
nosubs=0
for (i in myarr)
   nosubs++ # determine number of array elements
}
{
for(i=1; i<= nosubs; i++)
   if (i in myarr)
      sub("[|]", myarr[i])

print $0
} ' myfile


Report •

Related Solutions

#4
June 17, 2011 at 00:24:52
Thanx the awk was exactly what I needed!

Just one more question please:

How do I insert with awk a in front of the line e.g:

my name is Peter

Output should be:

HELLO my name is Peter


Report •

#5
June 17, 2011 at 07:15:05
✔ Best Answer
First, you're welcome. Glad to help.

Next, there are numerous ways to do this; this stub prints HELLO in front of every line of file: data.txt

awk ' { print "HELLO "$0 } ' data.txt

This is my favorite awk tutorial:

http://www.grymoire.com/Unix/Awk.html


Report •

#6
June 19, 2011 at 23:05:27
Thanx again so much for your help!

Report •

Ask Question