![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| String Replacement Script | jbud | Shell Programming and Scripting | 6 | 11-15-2007 06:37 AM |
| Need shell/sed script for grep+string replacement | pranavagarwal | Shell Programming and Scripting | 3 | 11-15-2007 06:35 AM |
| read string, check string length and cut | ozzy80 | Shell Programming and Scripting | 9 | 03-21-2007 05:56 PM |
| String Replacement with Perl | Lindarella | Shell Programming and Scripting | 4 | 09-29-2006 02:05 PM |
| String replacement in multiple files | WABonnett | Shell Programming and Scripting | 2 | 02-17-2004 03:49 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | 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. |
|
|||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|