|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | 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. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
Last edited by radoulov; 05-21-2012 at 11:31 AM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|