![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| String Replacement Script | jbud | Shell Programming and Scripting | 6 | 11-15-2007 02:37 AM |
| Need shell/sed script for grep+string replacement | pranavagarwal | Shell Programming and Scripting | 3 | 11-15-2007 02:35 AM |
| read string, check string length and cut | ozzy80 | Shell Programming and Scripting | 9 | 03-21-2007 01:56 PM |
| String Replacement with Perl | Lindarella | Shell Programming and Scripting | 4 | 09-29-2006 11:05 AM |
| String replacement in multiple files | WABonnett | Shell Programming and Scripting | 2 | 02-17-2004 11:49 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
sed problem - replacement string should be same length as matching string.
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. |
| Forum Sponsor | ||
|
|
|
||||
|
Quote:
Code:
[/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]$
Vino |
| Thread Tools | |
| Display Modes | |
|
|