Replace a text with a particular column and particular line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a text with a particular column and particular line
# 1  
Old 08-05-2011
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 not aware that (3,2) non-white space text is 88. It can be any number in general.


I tried to do with sed. But yet not succeeded. Smilie

Thank you.

* test.txt file attached.
# 2  
Old 08-05-2011
Code:
awk -v line=3 -v col=2 '{ if(NR==line) { $col=33}; print $0}'   inputfile

# 3  
Old 08-05-2011
Thanks. I guess, it would be 99 instead of 33 for my problem.

However, could you explain the syntax and arguments, e.g. if(NR==line)?

Thank you very much.
# 4  
Old 08-05-2011
Quote:
Originally Posted by hbar
Thanks. I guess, it would be 99 instead of 33 for my problem.

However, could you explain the syntax and arguments, e.g. if(NR==line)?

Thank you very much.
NR = Number Of Records (simply lines), "line" is a variable which is given the value "3" with -v option of AWK at the beginning.

also some links:

https://www.unix.com/emergency-unix-l...#post302409584
# 5  
Old 08-07-2011
Thanks a lot, Jim and Eagle.

---------- Post updated at 03:35 PM ---------- Previous update was at 02:39 PM ----------

Sorry, I think, I'm still having a problem.

As I said, in my example,

cat test.txt gives

10 200
5 600 1000
4 88 9
3
6 66 101

Now I wish to line=2,col=1 element, i.e. 5 by a variable.

I define the variable as follows.

var=99

Now if I do

awk -v line=2 -v col=1 '{ if(NR==line) { $col=$var}; print $0}' test.txt ,

I'm getting

10 200
5 600 1000 600 1000
4 88 9
3
6 66 101

whereas I was expecting

10 200
99 600 1000
4 88 9
3
6 66 101


What's going wrong? Thanks.
# 6  
Old 08-07-2011
Hi,

Try:
Code:
$ awk -v line=2 -v col=1 -v var=99 '{ if(NR==line) { $col=var}; print $0}' test.txt
10 200                                                                                                                                                                                                                                       
99 600 1000                                                                                                                                                                                                                                  
4 88 9                                                                                                                                                                                                                                       
3                                                                                                                                                                                                                                            
6 66 101

Regards,
Birei
# 7  
Old 08-07-2011
@Birei

Sorry I cannot assign var=99 in the same line, since it's going to be updated through a for loop.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. Shell Programming and Scripting

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: 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... (2 Replies)
Discussion started by: techieg
2 Replies

5. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 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 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

8. Shell Programming and Scripting

Replace text in column

Hi: I have a file as below: #LINE 3841.0 14663859 358272.0 676600.0 20.00 1510.00 91.00 1534.00 215.00 1624.00 374.00 1688.00 14663899 ... (2 Replies)
Discussion started by: total_ysf
2 Replies

9. Shell Programming and Scripting

Replace text based on column

Replace these if the column is - p = q q = p r = s s = r input - PQRSSP + PQRS output - QPSRRQ + PQRS Some thing,like (5 Replies)
Discussion started by: bumblebee_2010
5 Replies

10. UNIX for Dummies Questions & Answers

how to replace a text of line with a comment line

I want to replace this line : "test compare visible] true" and make it "#test compare visible] true". How can I do it ? And it should be checked in many sub folder files also. (6 Replies)
Discussion started by: manoj.b
6 Replies
Login or Register to Ask a Question