sed substitution for specific record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed substitution for specific record
# 1  
Old 05-21-2012
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 Smilie

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

Last edited by radoulov; 05-21-2012 at 12:31 PM..
# 2  
Old 05-21-2012
Can you pls post some input that support your requirement and desired output.
This User Gave Thanks to 47shailesh For This Post:
# 3  
Old 05-21-2012
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
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
This User Gave Thanks to bakunin For This Post:
# 5  
Old 05-21-2012
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'.
This User Gave Thanks to mirni For This Post:
# 6  
Old 05-21-2012
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove footer record in specific condition

Hi Experts, we have a requirement , need your help to remove the footer record in the file. Input file : 1011070375,,21,,NG,NG,asdfsfadf,1011,,30/09/2017,ACI,USD,,0.28,,,,,,,,,,,, 1011070381,,21,,NG,NG,sgfseasdf,1011,,30/09/2017,ACI,GBP,,0.22,,,,,,,,,,,,... (6 Replies)
Discussion started by: KK230689
6 Replies

2. Shell Programming and Scripting

Help with print out line that have different record in specific column

Input file 1: - 7367 8198 - 8225 9383 + 9570 10353 Input file 2: - 2917 3667 - 3851 4250 + 4517 6302 + 6302 6740 + 6768 7524 + 7648 8170 + 8272 8896 + 8908 9915 - 10010 ... (18 Replies)
Discussion started by: perl_beginner
18 Replies

3. Shell Programming and Scripting

Get last field specific record

i have file A as below contents --------------------------- Use descriptive thread titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt". For example, do not post questions For example, do not deliminated. output file as below:... (2 Replies)
Discussion started by: ANSHUMAN1983
2 Replies

4. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

5. Shell Programming and Scripting

Regarding multiline record searching with specific pattern

Dear Experts, I need to extract specific records from one file which has multiline records. Input file pattern is: ============ aaaaaaaa bbbbbbbb asdf 1234 cccccccc dddddddd ============ aaaaaaaa bbbbbbbb qwer 2345 cccccccc dddddddd (7 Replies)
Discussion started by: dhiraj4mann
7 Replies

6. Shell Programming and Scripting

sed substitution

Using sed I'm trying to replace 'string' with ']' while retaining case and ignoring words with 'string' in it along with additional characters like 'strings' and those which already contain the ] wrapper. I'm hoping to do it with sed and the right expression, if possible. Example: Apple... (2 Replies)
Discussion started by: tom.lee
2 Replies

7. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

8. Shell Programming and Scripting

Computing dataset for a specific record

Hello everybody, I want to compute a data file in awk. I am new in awk and I need your help. The data file has the following fields. It has thousands of records. Col1 Col2 Col3 Col4 Col5 0.85 0.07 Fre 42:86 25 0.73 0.03 frp 21:10 28 0.64... (12 Replies)
Discussion started by: ubeejani
12 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

10. Shell Programming and Scripting

a specific string substitution

hi guys...need some help here... i am making a a script to automatically install netbackup client...so its gonna write a configuration file according to the host name.... the line would be something like this CLIENT_NAME = odel_bkp.test.com the thing is ...the host name in reallity is... (2 Replies)
Discussion started by: razael
2 Replies
Login or Register to Ask a Question