how to change one line in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to change one line in a file?
# 1  
Old 02-18-2009
how to change one line in a file?

Suppose I have a file. The content of the file is:
Fwf33
#enable_a STH.
fwqfw
egvega

What I want to do is: if "STH" = "YES", do not change it, otherwise, change it to "YES". e.g.

if
“enable_a ERR”
change it to be
“enable_a YES”

if
“enable_a YES”
do not change the file.

How can I implement this using perl, sed, or other unix command?
Thanks,
# 2  
Old 02-18-2009
Code:
sed 's/enable_a 	ERR/enable_a 	YES/' FILE > NEWFILE

# 3  
Old 02-18-2009
Code:
awk '/enable_a/ && $NF=="ERR"{$NF="YES"}1'  FILE > NEWFILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change file section into each line?

Hi Gurus, I have below file which has different sections, need to move the sections to beginning of the each record. original file aaa bbb ccc ddd eee fff output file. aaa bbb ccc ddd eee fff (6 Replies)
Discussion started by: green_k
6 Replies

2. UNIX for Beginners Questions & Answers

How to change name of the file with first line of the file which has some unwanted text in it?

I have a log file, which i have divided into 14 files using csplit, the file looks like below test-000000 test-000001 #and so on until 14 now I want all the 14 files generated to be renamed as the some part of test in first line of the file how can i eliminate the unwanted text? sample... (5 Replies)
Discussion started by: Sekhar419
5 Replies

3. Shell Programming and Scripting

Search a string in a file and change another in the line

I did it myself (0 Replies)
Discussion started by: apenkov
0 Replies

4. Shell Programming and Scripting

Replacing last line with awk and change the file name

Hi Guys, I am having a set of date format files files where I am performing the below set of operations in the files . I Need to replace the last line value with specific date which is a pipe delimited file. for egf1_20140101.txt aa|aus|20140101|yy bb|nz|20140101|yy . .... (19 Replies)
Discussion started by: rohit_shinez
19 Replies

5. Shell Programming and Scripting

Need script to change a line in file....

Hello all, I have a line of code in a file that I need to change in the /etc/sysconfig/kdump file presently the line reads: KDUMP_COMMANDLINE_APPEND="irqpoll nr_cpus=1 reset_devices cgroup_disable=memory mce=off" what I need to do is put a comment out the 1st line and repeat it, and... (5 Replies)
Discussion started by: gartie
5 Replies

6. Shell Programming and Scripting

Change in last line of each part of a file

Hi, I have a input files as below. Sample of one input file type mtg_key = record decimal("\344") id; string("\344") ct_cd; string("\344") st_cd; end; type psl_key = record decimal("\344") id; utf8 string("\344") st_cd; end; type amk_fields = record ... (6 Replies)
Discussion started by: adgangwar
6 Replies

7. UNIX for Dummies Questions & Answers

sed question - change 2d to last line in file

The file is repeating blocks of text separated by a blank line. Some of the lines are unique. e.g.: This is unique line This is line 2 This is line 3 This is line 4 This is unique line This is line 2 This is line 3 This is line 4 ... The goal is to change/replace line 4... (2 Replies)
Discussion started by: uiop44
2 Replies

8. Shell Programming and Scripting

awk one line, change \n to sth in a file

Hi Everyone, cat 1.txt aaa bbb ccc outout will be cat 2.txt ,,aaa,,bbb,ccc,, means change "\n" to ",,", and add ",," into the beginging and ending. right now i am using perl while to open and read the file, then split \t, feel not nice. please advice. and i hear using perl... (8 Replies)
Discussion started by: jimmy_y
8 Replies

9. Shell Programming and Scripting

Script to change file contents line by line

Hi, I'm struggling to write a script to do the following, -will go through each line in the file -in a specific character positions, changes the value to a new value -These character positions are fixed througout the file ----------------------- e.g.: file1.sh will have the following 3... (4 Replies)
Discussion started by: vini99
4 Replies

10. UNIX for Dummies Questions & Answers

Change Specific Line of a File

Hi everyone, I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right... (10 Replies)
Discussion started by: msb65
10 Replies
Login or Register to Ask a Question