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


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-21-2012
Registered User
 
Join Date: May 2012
Posts: 12
Thanks: 7
Thanked 0 Times in 0 Posts
Error sed substitution for specific record

Hi, I have a file with two different people, each with there own figure next to it. How would I use a sed command to try and change the correct corresponding value? or is another command more appropriate? I have tried


Code:
sed -n '/dan/p' money | sed -i 's/1000/1500/g' money

the file contents are as follows


Code:
dan    1000
john   1200

this does not seem to work in such a way that if both numeric values were identical that only the relevant one would be changed

Moderator's Comments:
Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 05-21-2012 at 11:31 AM..
Sponsored Links
    #2  
Old 05-21-2012
Registered User
 
Join Date: Jul 2008
Location: NY
Posts: 203
Thanks: 13
Thanked 39 Times in 38 Posts
Can you pls post some input that support your requirement and desired output.
The Following User Says Thank You to 47shailesh For This Useful Post:
somersetdan (05-21-2012)
Sponsored Links
    #3  
Old 05-21-2012
Registered User
 
Join Date: May 2012
Posts: 12
Thanks: 7
Thanked 0 Times in 0 Posts
What im tring to do is add a value onto each persons value and then update the original file which holds this data. Im aware that if i dont specify the correct record then if two values are the same it could lead to both being updated. I hope that helps



Code:
a=`sed -n '/dan/p' money | cut -f2`
b=100
c=`expr $a + $b`

echo "your new balance is $c"
sed -n '/dan/p' money | sed -i 's/1000/1500/g' money

    #4  
Old 05-21-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,292
Thanks: 27
Thanked 450 Times in 351 Posts
First: if your input file and output file are the same than sed will destroy the file. Use a different file for output.

Second: have a look at the man page for sed, about line ranges. You also might want to read this thread for an explanation and an example for how this works.

I hope this helps.

bakunin
The Following User Says Thank You to bakunin For This Useful Post:
somersetdan (05-21-2012)
Sponsored Links
    #5  
Old 05-21-2012
Registered User
 
Join Date: Mar 2011
Posts: 679
Thanks: 47
Thanked 176 Times in 169 Posts
I think what you want is:

Code:
sed '/dan/ s/1000/1500/'

This will do the substitution only on the lines that match 'dan'.
Note that if a line contains "Maydan" or something, it will change this also, so it's safer to use anchors:

Code:
sed '/^dan/ s/1000/1500/'

which will match only lines starting with 'dan'.
The Following User Says Thank You to mirni For This Useful Post:
somersetdan (05-21-2012)
Sponsored Links
    #6  
Old 05-21-2012
Registered User
 
Join Date: Jul 2008
Location: NY
Posts: 203
Thanks: 13
Thanked 39 Times in 38 Posts
or you can do like this
Code:
$cat infile
dan 1000
john 1200
jim 100
marc 5000
hazel 25
mitch 5000


Code:
incr=5000
awk -v bal="$incr" '$1=="dan" && $2=$2+bal;$1!="dan"' infile

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reject the record if the record in the next line does not begin with 2. supchand Shell Programming and Scripting 5 10-16-2011 04:52 PM
Reject the record if the record in the next line does not satisfy the pattern supchand Shell Programming and Scripting 2 10-16-2011 01:15 AM
Regarding multiline record searching with specific pattern dhiraj4mann Shell Programming and Scripting 7 05-08-2011 05:18 AM
Computing dataset for a specific record ubeejani Shell Programming and Scripting 12 07-17-2009 07:29 AM
a specific string substitution razael Shell Programming and Scripting 2 02-04-2009 12:54 PM



All times are GMT -4. The time now is 10:36 AM.