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
# 8  
Old 11-26-2011
Code:
awk -F: 'FNR==linenum{if(new_mileage-$10>=4000)$10=new_mileage}1' linenum=$linenum new_mileage=$new_mileage OFS":" input_file

1 => is for printing the output.

--ahamed
# 9  
Old 11-27-2011
I tried using the exact code above and got an error could not find file OFS etc before attempting to modify it to work for my particular case.
awk: cmd. line:1: fatal cannot open file `OFS`: for reading (No such file or directory)


I already have the action in a conditional statement, so I just need to modify the data on the variable line number in the 10th field and have this written to the file, not just displayed on screen.

Code:
if [ "$rout_difference" -ge "$routine_maint" ]; then
            echo "Routine maintenance is required on this bike"
            echo "Would you like to perform this maintenance and update your records?"
            echo "Please enter Yes to confirm, [RETURN] to skip"
            read confirm_routine
                if [ "$confirm_routine" == "Yes" -o "yes" -o "YES" ]; then
                    echo "Updating routine maintenance record"
                    awk -F: 'FNR==linenum{$10=new_mileage}1' linenum=$1 new_mileage=$current_new_mileage OFS":" motorcycles.txt
                fi


Last edited by Franklin52; 11-28-2011 at 03:26 PM..
# 10  
Old 11-27-2011
A bug... try this...
Code:
awk -F: 'FNR==linenum{if(new_mileage-$10>=4000)$10=new_mileage}1' linenum=$linenum new_mileage=$new_mileage OFS=":" input_file

--ahamed
# 11  
Old 11-27-2011
Still no luck.

ex record in file:
2005:Suzuki:GS500f:Blue:6500:4000:6500:6500:6500:6500

commands runs but does not update
# 12  
Old 11-27-2011
Code:
root@bt:/tmp# cat input_file
2005:Suzuki:GS500f:Blue:6500:4000:6500:6500:6500:6500

root@bt:/tmp# awk -F: 'FNR==linenum{if(new_mileage-$10>=4000)$10=new_mileage}1' linenum=1 new_mileage=12000 OFS=":" input_file
2005:Suzuki:GS500f:Blue:6500:4000:6500:6500:6500:12000

--ahamed
# 13  
Old 11-27-2011
It displays correctly but does not update the file
# 14  
Old 11-27-2011
It will not... You have to redirect the output to a temp file and then rename it...
Code:
awk '{...}' file > temp_file
mv -f temp_file file

--ahamed
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