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
# 8  
Old 08-07-2011
Something like this??
Code:
$ cat script.sh
#!/usr/bin/env bash

number=99
awk -v line=2 -v col=1 -v var=$number '{ if(NR==line) { $col=var}; print $0}' text.txt
$ bash script.sh
10 200
99 600 1000
4 88 9
3
6 66 101

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

It works now. Thanks a lot.

If I have a long command how can I break that in a text editor (e.g. I've been using vi editor) so that scripting instruction is obeyed ?

Like I want to write
awk -v line=2 -v col=1 -v var=$number <break>

'{ if(NR==line) { $col=var}; print $0}' test.txt
# 10  
Old 08-07-2011
Try:
Code:
awk -v line=2 -v col=1 -v var=$number \
        '{ if(NR==line) { $col=var }; print $0}' test.txt

Regards,
Birei
# 11  
Old 08-07-2011
Code:
line=2
col=1
var=99
awk -v line=$line -v col=$col -v var=$var 'NR==line{$col=var}1' file

# 12  
Old 08-07-2011
@birei

This is a bit strange.

When I copied your lines and pasted and then ran the shell script,it worked fine.

But if I type them line-wise, it shows error.

However, if I write the whole command together and create the second line by pressing Enter key, it works again!

I did it in vi editor. I wondered how the way of writing (pressing enter to make a new line or write as a fresh on the next line) makes difference.
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