Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 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 to Reply

« Previous Thread | Next 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
awk to replace a specific field in certain condition mady135 Shell Programming and Scripting 7 11-24-2014 11:02 PM
How to replace and remove few junk characters from a specific field? snemuk14 UNIX for Dummies Questions & Answers 10 10-01-2014 11:32 AM
How to print with awk specific field different from specific character? elilmal Shell Programming and Scripting 4 08-22-2013 04:23 AM
awk search/replace specific field, using variables for regexp & subsitution then overwrite file cyphex Shell Programming and Scripting 3 09-23-2012 05:46 AM
Using awk to read a specific line and a specific field on that line. Bungkai Shell Programming and Scripting 6 07-31-2011 01:13 PM
awk how to replace specific field with new value setepo Shell Programming and Scripting 10 04-07-2011 10:17 AM
replacing field in specific line in a file vytenis Shell Programming and Scripting 5 09-01-2010 08:06 PM
Find and replace a string a specific value in specific location in AIX techmoris Shell Programming and Scripting 5 03-11-2010 06:24 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 07: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 09:40 PM


All times are GMT -4. The time now is 02:38 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password