Hi guys, I hope you can help me with my problem.
I have a text file that contains lines like this:
78 ANGELO -809.05
79 ANGELO2 -5,000.06
I need to find all occurences of amounts that are negative and replace them with x's
78 ANGELO xxxxxxx
79 ANGELO2 xxxxxxxxx
Note that the number of x's should match the number of characters replaced, that is, since -809.05 is 7 chars long, it should be replaced by 7x's. The x's should also be in the same position as where the original negative sign was to preserve the line spacing of the file.
Any idea how to do this? Thanks for your help.
There is probably a more effecient way but this was easy:
awk '$NF >= 0 {print}
$NF < 0 {gsub(/./,"x",$NF) ; print}' yourdata
How about just using bash builtins?
# ./amangeles.sh
78 ANGELO xxxxxxx
79 ANGELO2 xxxxxxxxx
# cat ./amangeles.sh
#! /bin/bash
while read line; do
set -- ${line}
echo "${1} ${2} ${3//?/x}"
done < amangeles.txt
exit 0
Cheers
ZB
vino
January 11, 2006, 4:13am
4
zazzybob:
How about just using bash builtins?
# ./amangeles.sh
78 ANGELO xxxxxxx
79 ANGELO2 xxxxxxxxx
# cat ./amangeles.sh
#! /bin/bash
while read line; do
set -- ${line}
echo "${1} ${2} ${3//?/x}"
done < amangeles.txt
exit 0
Cheers
ZB
ZB, your script will replace the numbers irrespective of whether they are positive or negative. Your script enhanced..
[/tmp]$ cat sub.txt
78 ANGELO -809.05
77 ANGELO2 5,000.06
79 ANGELO2 -5,000.06
[/tmp]$ ./sub.sh
78 ANGELO xxxxxxx
77 ANGELO2 5,000.06
79 ANGELO2 xxxxxxxxx
[/tmp]$ cat sub.sh
#! /bin/sh
while read line; do
set -- ${line}
[[ "${3}" == *-* ]] && echo "${1} ${2} ${3//?/x}" || echo "${line}"
done < sub.txt
[/tmp]$
Cheers'
Vino
Good catch Vino - my bad there - didn't read the original post properly :o
Cheers
ZB