The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts 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 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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2006
Registered User
 

Join Date: Jan 2006
Posts: 1
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-10-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
There is probably a more effecient way but this was easy:
Code:
awk '$NF >= 0 {print}
     $NF < 0 {gsub(/./,"x",$NF) ; print}' yourdata
Reply With Quote
  #3 (permalink)  
Old 01-10-2006
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
How about just using bash builtins?

Code:
# ./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
Reply With Quote
  #4 (permalink)  
Old 01-11-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,664
Quote:
Originally Posted by zazzybob
How about just using bash builtins?

Code:
# ./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..

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]$
Cheers'
Vino
Reply With Quote
  #5 (permalink)  
Old 01-11-2006
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Good catch Vino - my bad there - didn't read the original post properly

Cheers
ZB
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0