Replace a Column Within a Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a Column Within a Line
# 1  
Old 05-12-2013
Replace a Column Within a Line

Hello All,

I am trying to replace a column within a line that has a certain fruit name. I have a bunch of line such as:


Code:
apple:green:5cents:CA
pineapple:red:4cents:FL
orange:green:6cents:HI

...and change the third column in the specific line that matches the $fruitname variable to update the price.

So far, I have the following;

Code:
myfunc() {
     echo "\nPlease enter the new price."
     read newprice
     grep -i $fruitname fruitfile | awk 'BEGIN { FS=OFS=":"} { $3=$newprice; print}' | tee tempfile
}

myfunc $fruitname

...and the result I ma getting is that it is placing entire lines in the spot that the third column as opposed to simply updating the price column only.

My other option is to use sed instead of awk above, such as

Code:
sed "s/$oldprice/$newprice/" | tee tempfile

...but it would require one to also enter the current price, and it results in a blank column but everything else stays as is. Any idea?

Last edited by techieg; 05-12-2013 at 05:13 PM..
# 2  
Old 05-12-2013
You supply the fruit name as a parameter to myfunc PLUS you read it from stdin. On the other hand, I can't see the newprice variable populated nor passed to awk. And, if you enter "apple", both red and green apples will get the new price, should it contain some value, and should awk be passed that value.
All other lines not containing fruitname will be suppressed.
Not sure that this is what you intend to do...

In your sed solution, use a regex address to modify the fruitname containing lines only, and yes, the old price needs to be entered as well.
# 3  
Old 05-12-2013
Quote:
Originally Posted by RudiC
You supply the fruit name as a parameter to myfunc PLUS you read it from stdin. On the other hand, I can't see the newprice variable populated nor passed to awk. And, if you enter "apple", both red and green apples will get the new price, should it contain some value, and should awk be passed that value.
All other lines not containing fruitname will be suppressed.
Not sure that this is what you intend to do...

In your sed solution, use a regex address to modify the fruitname containing lines only, and yes, the old price needs to be entered as well.
Thanks for the prompt response. I updated the fruit types, one of the "apple" columns should have been "pineapple", which I have updated, also updated the newprice variable, which was supposed to be the prompt, not for $fruitname variable which was already passed to the function. The results are as originally stated.

---------- Post updated at 04:19 PM ---------- Previous update was at 03:55 PM ----------

I figured it out, it was a typo all along. The sed replace code was fin after all:

Code:
sed "s/$oldprice/$newprice/" | tee tempfile


Last edited by techieg; 05-12-2013 at 05:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Replace Value of nth Column of Each Line Using Array

Hello All, I am writing a shell script with following requirement: 1. I have one input file as below CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 Replies

3. Shell Programming and Scripting

To replace a column of particular line

hi, I am having a file where in i need to replace a particular coulmn of certain specific line only for eg: a|xx|yy|xx b|aaa|qq|1234 c|11|aa|qq i need to replace 2nd line a|xx|yy|xx b|aaa|REPLACE|1234 c|11|aa|qq awk 'BEGIN{OFS=FS="|"}{print $3}'= 'REPLACE' {print}' (3 Replies)
Discussion started by: rohit_shinez
3 Replies

4. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

5. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

6. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file File1.txt"2011-11-02","Georgia","Atlanta","x","","" "2011-11-03","California","Los Angeles","x","","" "2011-11-04","Georgia","Atlanta","x","x","x" "2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
Discussion started by: charles33
4 Replies

7. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

8. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

9. Shell Programming and Scripting

Replace a text with a particular column and particular line

I have a file test.txt cat test.txt shows 10 200 5 600 1000 4 88 9 3 6 66 101 Suppose, I want to replace a number with a particular line and column. For instance, the number 88 in line 3, column 2. I want to replace it by 99. How can I do that? Note that I am... (11 Replies)
Discussion started by: hbar
11 Replies

10. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies
Login or Register to Ask a Question