Modify one line in a plain text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify one line in a plain text file
# 1  
Old 01-23-2013
Modify one line in a plain text file

Hi everyone,

I want to know, if there is a way to modify one line in a text file with unix script, with out re-writing all the file.

For example, i have this file:

Code:
CONFIGURATION_1=XXXX
CONFIGURATION_2=YYYY
CONFIGURATION_3=ZZZZ

supose i have a command or function "modify" that takes a file and modficates one line. "Modify" takes two arguments, the number of the line and the new value.

Code:
#script
modify 2 CONFIGURATION_2=XXXX

The result that i want on the file:

Code:
CONFIGURATION_1=XXXX
CONFIGURATION_2=XXXX
CONFIGURATION_3=ZZZZ

Thanks!

Sorry for my bad english.

Last edited by joeyg; 01-24-2013 at 01:11 PM.. Reason: Corrected title spelling
# 2  
Old 01-23-2013
Code:
line=$1
param=$2

awk -v L="$line" -v P="$param" 'NR==L{$0=P}1' file

# 3  
Old 01-23-2013
Nice! Supose other situation, the first parameter is nor the number of line, it is a string. For example:

Code:
CONFIGURATION_1=XXXX
CONFIGURATION_2=YYYY
CONFIGURATION_3=ZZZZ

supose i have a command or function "modify" that takes a file and modficates one line. "Modify" takes two arguments, a string and the new value. I want replace the line that contains a string "CONFIGURATION_2" with the value "CONFIGURATION_2=XXXX". Something like grep but writting.


Code:
#script
modify CONFIGURATION_2 CONFIGURATION_2=XXXX

The result that i want on the file:


Code:
CONFIGURATION_1=XXXX
CONFIGURATION_2=XXXX
CONFIGURATION_3=ZZZZ

Thanks!
# 4  
Old 01-23-2013
Code:
param_1=$1
param_2=$2

awk -F= -v P1="$param_1" -v P2="$param_2" '$1==P1{$0=P2}1' file

# 5  
Old 01-24-2013
Excelent! Thanks a lot!

---------- Post updated 01-24-13 at 08:00 AM ---------- Previous update was 01-23-13 at 03:53 PM ----------

One dude, it is posible modify the comand if the file = has space after or before, for examplo it is posible the file have this format:

Code:
PARAM1=XXXX
PARAM2           =AAAA
PARAM3       =        BBBB

The comand just found with the first line.

Thanks!
# 6  
Old 01-24-2013
Use awk gsub function to truncate those extra spaces:
Code:
awk '{gsub(" ","")}1' file

# 7  
Old 01-24-2013
Quote:
Originally Posted by bipinajith
Use awk gsub function to truncate those extra spaces:
Code:
awk '{gsub(" ","")}1' file

Where do i put this in the original command?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. Shell Programming and Scripting

Modify text file using sed

Hello all, I have some text files I need to do the following on: Delete banner page (lines 1-56) --I am doing this using sed Remove ^M --I am doing this using vi Remove trailer page --this can vary based on the contents of the file, it usually starts with *************************** I am... (5 Replies)
Discussion started by: jeffs42885
5 Replies

3. Shell Programming and Scripting

Sum numeric columns contained in a plain text file

Hi everyone, Here are the contents of a plain text file created by a SQL query: SUM(T.TRNQTY) COUNT(D.TRNSEQ) ---------------- ---------------- 1380 46 1393 59 2680 134 740 37 ... (5 Replies)
Discussion started by: gacanepa
5 Replies

4. Shell Programming and Scripting

Modify text file using awk

I have text file with lines as shown here. Each row has 11 columns separated by tab. In each row, i want to split the 8th column such that the output should look like shown below. Here value in the 9th column is DP value and in the 10th column is MQ value followed by the values after resource.EFF=.... (15 Replies)
Discussion started by: mehar
15 Replies

5. Shell Programming and Scripting

Modify the text file by script

Hi All the Helpers! I have a text file which looks like input.txt.I would request to please suggest me how can I make this file look like output.txt input.txt VOP 111 0 1 2 DEM 111 0 222 333 444 555 879 888 987 888 989 VOP 118 0... (2 Replies)
Discussion started by: Indra2011
2 Replies

6. Shell Programming and Scripting

from one word for line to plain text

Hello! I've got a very big file (from tokenization) which has one word for line. How is it possible then to rebuild the "original" text, knowing that <s> and </s> are the sentence-delimiters? My file looks like this: <s> && tanzania na Afrika kwa ujumla ambiwa na taifa kubwa... (6 Replies)
Discussion started by: mjomba
6 Replies

7. UNIX for Dummies Questions & Answers

Modify Text File

Hi, I would like to remove any lines from a text file that begin with #, or that are blank. How can I do that with BASH? Mike (3 Replies)
Discussion started by: msb65
3 Replies

8. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

Modify Specific Line of a Text File

Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you. Mike (5 Replies)
Discussion started by: msb65
5 Replies

10. Shell Programming and Scripting

Modify a text or xml file

Hi all, I want to write a shell which would allow me to edit a text file or a xml file. Basically i want to add a new node in a existing xml file. The values for this new node are based on user input. Thanks in advance Zing (9 Replies)
Discussion started by: zing
9 Replies
Login or Register to Ask a Question