replace a line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace a line in a file
# 8  
Old 03-14-2009
Code:
  awk -v var="12345" ' 
  /ABC/ {sub ($3, var)}
  {print}
  ' $file > $file.temp
  mv -f $file.temp $file

I'm having some issues with this part because sometimes there's alot of the same values.

Example:

cat test.file
ABC 0 0 0 0 0 313131 0 0 13131313 0 34214325 0234234

Applying the above command would result in:

cat test.file
ABC 12345 0 0 0 0 313131 0 0 13131313 0 34214325 0234234

while I need it to be:

ABC 0 12345 0 0 0 0 313131 0 0 13131313 0 34214325 0234234

How to fix?
# 9  
Old 03-14-2009
The code should be:

Code:
awk -v var="12345" ' 
  /ABC/ {$3=var}
  {print}
  ' $file > $file.temp
  mv -f $file.temp $file

Regards
# 10  
Old 03-14-2009
Works like a harm, thank you Franklin.
# 11  
Old 03-16-2009
Code:
line: the specific line to be replaced
col:  the specific column to be replaced
val:  the value going to replace others
nawk -v line=3 -v col=4 -v val="---" 'NR==line{$col=val}1' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace line for another line in file

Hey, i have been struggeling to find the answer myself so this is the best forum has alwyas help me :) i have an API that get data from a provider via curl. the data comes like this : AccountCode1234 CompneyNameA Credit: 200 AccountCode5678 CompneyNameB Credit: 200 AccountCode999... (3 Replies)
Discussion started by: batchenr
3 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

4. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

5. Shell Programming and Scripting

Replace Each Line Of A File

Hi All, I want to repalce each line of a file by a constant value using awk. I do not need to match the pattern and replace and I also do not need to perform replace for selected lines of file. Every line in the file will be replace by the constant value. I searched every where and did not... (2 Replies)
Discussion started by: angshuman
2 Replies

6. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

7. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

8. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

9. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

10. Shell Programming and Scripting

how to replace a line in file with blank line

Hi I nned cmd to which will help me to replace a line in file with blank line e.g. file1 a b c d e after running cmd I shud get file1 b c d e (5 Replies)
Discussion started by: tarunn.dubeyy
5 Replies
Login or Register to Ask a Question