Operating System - HP-UX
1753681 Members
5913 Online
108799 Solutions
New Discussion юеВ

replace words with spaces

 
Pat Peter
Occasional Advisor

replace words with spaces

Hi,

I have a file which contains lines with the following format:

S12|abc|123|456|111|999|

I need to replace the word or number which comes between 5th and 6th "|" (our case '999') with a space.

It would be great if it can be done for lines that start with "S".

Please help.

Thanks,
Pat

8 REPLIES 8
Biswajit Tripathy
Honored Contributor

Re: replace words with spaces

One way would be:
cat your_file | grep ^S | awk -F\| '{printf("%s|%s|%s|%s|%s| |\n",$1,$2,$3,$4,$5)}'

- Biswajit
:-)
Vibhor Kumar Agarwal
Esteemed Contributor

Re: replace words with spaces

sed -e '/^S/s/\(.*\)|\(.*\)|\(.*\)|\(.*\)|\(.*\)|\(.*\)|\(.*\)/\1|\2|\3|\4|\5| |\6/g' filename
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: replace words with spaces

You can do it as,

echo "S12|abc|123|456|111|999|" | awk -F"|" '/^S/ { $6=" ";OFS="|";print $0 }'

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: replace words with spaces

You can use perl as,

echo "S12|abc|123|456|111|999|" | perl -ne ' @arr=split(/\|/);$arr[5]=" ";$,="|";print @arr;'

or

perl -ne ' @arr=split(/\|/);$arr[5]=" ";$,="|";print @arr;'

hth.
Easy to suggest when don't know about the problem!
H.Merijn Brand (procura
Honored Contributor

Re: replace words with spaces

Almost correct perl solution. Here's an alternative, and some remarks

First, let's use autosplit (-aFpattern)

lt09:/tmp 118 > cat xx.txt
S12|abc|123|456|111|999|ABRACA
lt09:/tmp 119 > perl -naF\\\| -e'$,="|";/^S/||next;$F[5]=" ";print@F' xx.txt
S12|abc|123|456|111| |ABRACA
lt09:/tmp 120 >

Note that if the last field is empty, which in our case is not true: it contains a newline, split without additional arguments will drop them. You will have to add

split /\|/, $_, -1

to retain the empty trailing fields. -a does not need that, because the fields are split *before* the chomp happens:

lt09:/tmp 126 > perl -naF\\\| -e'chomp;$,="|";/^S/||next;$F[5]=" ";print@F' yy.txt
S12|abc|123|456|111| |
lt09:/tmp 127 > perl -ne 'chomp;@arr=split(/\|/);$arr[5]=" ";$,="|";print @arr;' yy.txt
S12|abc|123|456|111| lt09:/tmp 128 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: replace words with spaces

Even shorter, and retaining the lines that do not start with 'S':

lt09:/tmp 131 > cat xx.txt
S12|abc|123|456|111|999|ABRACA
T12|abc|123|456|111|989|ABRACA
lt09:/tmp 132 > perl -naF\\\| -e'$,="|";/^S/and$F[5]=" ";print@F' xx.txt
S12|abc|123|456|111| |ABRACA
T12|abc|123|456|111|989|ABRACA
lt09:/tmp 133 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Arturo Galbiati
Esteemed Contributor

Re: replace words with spaces

Hi,
awk -F"|" '/^S/ {$6=" ";OFS="|";print} file

HTH,
Art
Sandman!
Honored Contributor

Re: replace words with spaces

Pat,

Here is a short one-liner using awk. It will replace the string between the 5th and 6th columns if there is a leading "S" in the first column of your input file or else it will print the entire record.

# awk -F"|" '{if($1~/^S/){$6=" "; OFS="|"; print $0} else print $0}' input_file > output_file