Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Replace specific field on specific line sed or awk

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-26-2011   -   Original Discussion by crownedzero
crownedzero's Unix or Linux Image
crownedzero crownedzero is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2011, 3:55 PM EST
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 11-26-2011   -   Original Discussion by crownedzero
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 19 October 2016, 5:02 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts
Sample input and expected output will help us to help you...

--ahamed
Sponsored Links
    #3  
Old Unix and Linux 11-26-2011   -   Original Discussion by crownedzero
crownedzero's Unix or Linux Image
crownedzero crownedzero is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2011, 3:55 PM EST
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
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 Unix and Linux 11-26-2011   -   Original Discussion by crownedzero
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 19 October 2016, 5:02 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts
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
Sponsored Links
    #5  
Old Unix and Linux 11-26-2011   -   Original Discussion by crownedzero
crownedzero's Unix or Linux Image
crownedzero crownedzero is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2011, 3:55 PM EST
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old Unix and Linux 11-26-2011   -   Original Discussion by crownedzero
ahamed101's Unix or Linux Image
ahamed101 ahamed101 is offline Forum Advisor  
root is god!!!
 
Join Date: Sep 2008
Last Activity: 19 October 2016, 5:02 AM EDT
Location: San Jose, CA
Posts: 1,910
Thanks: 54
Thanked 488 Times in 481 Posts
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
Sponsored Links
    #7  
Old Unix and Linux 11-26-2011   -   Original Discussion by crownedzero
crownedzero's Unix or Linux Image
crownedzero crownedzero is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2011, 3:55 PM EST
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
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?
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using awk to read a specific line and a specific field on that line. Bungkai Shell Programming and Scripting 6 07-31-2011 02:13 PM
awk how to replace specific field with new value setepo Shell Programming and Scripting 10 04-07-2011 11:17 AM
replacing field in specific line in a file vytenis Shell Programming and Scripting 5 09-01-2010 09:06 PM
How to append a character to the last but one field on a specific line? npatwardhan Shell Programming and Scripting 2 02-03-2010 08:30 PM
using sed to replace a specific string on a specific line number using variables todd.cutting Shell Programming and Scripting 2 08-13-2009 10:40 PM



All times are GMT -4. The time now is 12:25 AM.