Replace specific field on specific line sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace specific field on specific line sed or awk
# 1  
Old 11-26-2011
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 arithmetic I'd like to replace the 10th field on the 4th line(assuming user chose 4) in the input file.

Any ideas? Thanks in advance
# 2  
Old 11-26-2011
Sample input and expected output will help us to help you...

--ahamed
# 3  
Old 11-26-2011
Update the mileage on a bike
assume line 4 is:
2005:Suzuki:GS500f:Blue:6500:6000:6500:6500:6500:7000

I've extracted the 10th field of the 4th line i.e. 7000.
User enters 12000 (new odometer reading)
If the difference is greater than 4000 (scheduled maintenance), I need to "service" the bike and update the mileage.

Replace 4th line, 10th field with new odometer reading
2005:Suzuki:GS500f:Blue:6500:6000:6500:6500:6500:12000
# 4  
Old 11-26-2011
Try this...
Code:
awk -F: '{if($0~bike&&new_mileage-$10>=4000){$10=new_mileage}}1' bike=Suzuki new_mileage=12000 OFS=":" input_file

or

Code:
#!/bin/bash

read -p "Which bike ? : " bike
read -p "Enter new mileage : " new_mileage
old_mileage=$( sed "/$bike/{s/.*:\([0-9]*\)$/\1/g}" input_file )
((diff=new_mileage-old_mileage))
if [ $diff -ge 4000 ]
then
        sed "/$bike/{s/$old_mileage$/$new_mileage/g}" input_file
fi

Use -i option with sed to make the changes inline i.e. directly

--ahamed
# 5  
Old 11-26-2011
Java

I have a large number of these not specific to values, so I was hoping to replace $# rather than specific values.

Also wouldn't the sed replace all the values? Thanks for the -i that might be what I was missing

Code:
    #prob pipe was not needed but I ccouldn't get the syntax, revisiting later
    old_mileage=$(awk -F':' '{print $10}' motorcycles.txt | awk -v line=$1 'FNR==line')
    routine_maint=$(awk -F':' '{print $6}' motorcycles.txt | awk -v line=$1 'FNR==line')
    (( difference=current_mileage-old_mileage ))
    
        if [ "$difference" -ge "$routine_maint" ]; then
            echo "Overdue"
            #do stuff here ie maintenance
        else
            echo "else"
        fi

But I have 5 different function i.e. front tire, rear tire, chain, etc and I'm hoping to just change the $# to update for the specific function
# 6  
Old 11-26-2011
You can replace with sed also but it might get difficult to form the regex. AWK will be pretty easy!

--ahamed

---------- Post updated at 02:16 PM ---------- Previous update was at 02:05 PM ----------

Code:
old_mileage=$( awk -F':' 'FNR==line{print $10}' line=$1 motorcycles.txt )
routine_maint=$( awk -F':' 'FNR==line{print $6}' line=$1 motorcycles.txt )

You don't need 2 awk in your code.
--ahamed
# 7  
Old 11-26-2011
awk -F: '{if($0~bike&&new_mileage-$10>=4000){$10=new_mileage}}1' bike=Suzuki new_mileage=12000 OFS=":" input_file

I could change this to awk -F: 'if{(FNR==linenum){$10=new_mileage}}1' linenum=$linenum new_mileage=$new_mileage OFS":" input_file, correct?

Quick question, the 1 following the $10=new_mileage, what's its purpose?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to multiple and replace in a specific line

Hi Folks, I have the file in which I need to multiply the content of a line and replace the initial content of that line with the obtained answer. For example if this is my input file file1.txt 2.259314750 xxxxxx 1.962774350 xxxxxx 2.916817290 xxxxxx 1.355026900 ... (4 Replies)
Discussion started by: Madiouma Ndiaye
4 Replies

2. Shell Programming and Scripting

awk to replace a specific field in certain condition

Hi, I have a file like below PRUM,67016800 ,CC ,C1,67016800 , ,Y,Y,2 ,CK,BX,FOX ,00000001,EA,00000001,20141120 00:00:00, ,N,Y,Y,CK ABCDEF... (7 Replies)
Discussion started by: mady135
7 Replies

3. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

sed to replace specific positions on line with file contents

Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80 in file1 is as follows: I want to replace the 000000000178800 (positions 34 - 49) on this file with the contents of... (2 Replies)
Discussion started by: nwalsh88
2 Replies

6. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

7. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

8. Shell Programming and Scripting

awk how to replace specific field with new value

I need to replace specific field (x) in a table with new value (y): Input: 1 2 3 4 5 x 6 7 8 9 0 0 Output: 1 2 3 4 5 y 6 7 8 9 0 0 I have no idea how to do this. (10 Replies)
Discussion started by: setepo
10 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. 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
Login or Register to Ask a Question