Edit file content at the specific line.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edit file content at the specific line.
# 1  
Old 03-18-2011
Edit file content at the specific line.

How to edit file content at the specific line? For example at below

The things to edit --> This is line 2. And it is below line 1.

Code:
This is line 1.       
This is line 2.     # i want to append some words at this row line. How?
This is line 3.

# 2  
Old 03-18-2011
Hi alvin0618,

Something like:

Code:
awk '{
if($0~"This is line 1") {
print; getline; print $0,"<<Words I have added>>" 
}
else 
print $0
}' inputfile
This is line 1.       
This is line 2.      <<Words I have added>>
This is line 3.

Regards
# 3  
Old 03-18-2011
Quote:
Originally Posted by cgkmal
Hi alvin0618,

Something like:

Code:
awk '{
if($0~"This is line 1") {
print; getline; print $0,"<<Words I have added>>" 
}
else 
print $0
}' inputfile
This is line 1.       
This is line 2.      <<Words I have added>>
This is line 3.

Regards
Hi, cgkmal. Thanks for the reply.

I follow the code you provide
the system prompts this errors
Code:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 4
awk: bailing out near line 4

# 4  
Old 03-18-2011
Through sed..
Code:
sed 's/line 2/& Append words here../' inputfile > outfile
or
sed '/line 1/{N;s/$/& Append words here/}' inputfile > outfile

# 5  
Old 03-18-2011
Quote:
Originally Posted by alvin0618
Hi, cgkmal. Thanks for the reply.

I follow the code you provide
the system prompts this errors
Code:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 4
awk: bailing out near line 4

alvin, please try directly with echo".." instead of a file and see if error appear, if so, in what position, like this:
Code:
echo "This is line 1.       
This is line 2.     
This is line 3." | awk '{if($0~"This is line 1") {print $0; getline; print $0,"<<Words I have added>>"} else print $0}'
This is line 1.       
This is line 2.      <<Words I have added>>
This is line 3.

Regards
# 6  
Old 03-18-2011
Add "anyword" at the end of line 2 :

Code:
# cat tst
a1111,222 2222,333333,22222 22,555555 555
b1111,2222222,333333,444444 4444444,55555
a1111,222 2222,333333,22222 22,555555 555
a1111,222 2222,333333,22222 22,555555 555
a1111,222 2222,333333,22222 22,555555 555
#

then
Code:
# printf "2s/.*/& anyword/\nw\nq\n" | ed -s tst

or
Code:
# printf "2s/$/ anyword/\nw\nq\n" | ed -s tst

will give

Code:
# cat tst
a1111,222 2222,333333,22222 22,555555 555
b1111,2222222,333333,444444 4444444,55555 anyword
a1111,222 2222,333333,22222 22,555555 555
a1111,222 2222,333333,22222 22,555555 555
a1111,222 2222,333333,22222 22,555555 555
#


Last edited by ctsgnb; 03-18-2011 at 05:49 AM..
# 7  
Old 03-18-2011
Quote:
Originally Posted by michaelrozar17
Through sed..
Code:
sed 's/line 2/& Append words here../' inputfile > outfile
or
sed '/line 1/{N;s/$/& Append words here/}' inputfile > outfile

Hi, ichaelrozar17
Thanks for the reply. It works perfectly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

2. UNIX for Dummies Questions & Answers

Deny to edit a specific file in sudoers

How do I deny a user to edit a specific file in directory but the user will have a capability to use sudo and execute any command? I will just deny him/her to edit sayy 5files in different directories in linux? example. He cannot edit /etc/modprobe.d/blacklist.conf and /etc/sshd.config? Then the... (6 Replies)
Discussion started by: lhareigh890
6 Replies

3. Shell Programming and Scripting

want to print the file content from the specific line

Hi All, I would like to print the content from the specific line of a file . For example... i have file abc.txt which has 100 lines of code ,from this file i would like to print the content from 20,19,18th line......like that Regards Srikanth (4 Replies)
Discussion started by: srikanthg
4 Replies

4. Shell Programming and Scripting

How to edit specific variable in file?

HI guys i have a question. Question 1: how do i modify a particular string? e.g echo "Please enter Book Title: " read a echo "Please enter Author: " read b if ] then echo " Record found!" which will then pop out a menu with the follow output 1. Update Name 2.... (1 Reply)
Discussion started by: ichar
1 Replies

5. Shell Programming and Scripting

Script to Check & Edit Content of a file (Addition of comma in each lines of code)

Hi all, I need to write an automated bash shell script which performs such operations: 1. Grep the header of everyline with the initial of "T" in "FILE_A" 2. Perform a for loop, Count the numbers of comma in the line of code, if (no. of comma < 17) ADD the comma until 17; ... (2 Replies)
Discussion started by: big_nutz
2 Replies

6. Shell Programming and Scripting

Extract specific content from a file

My input file: >sequence_1 ASSSSSSSSSSSDDDDDDDDDDDCCCCCCC ASDSFDFFDFDFFWERERERERFSDFESFSFD >sequence_2 ASDFDFDFFDDFFDFDSFDSFDFSDFSDFDSFASDSADSADASD ASDFFDFDFASFASFASFAFSFFSDASFASFASFAFS >sequence_3 VEDFGSDGSDGSDGSDGSDGSDGSDG dDFSDFSDFSDFSDFSDFSDFSDFSDF SDGFDGSFDGSGSDGSDGSDGSDGSDG My... (22 Replies)
Discussion started by: patrick87
22 Replies

7. Shell Programming and Scripting

Remove specific content in a file

Hi, I have a file called fl_list consists of files i have to archive. I want to create a exception parm called except_parm, so if it finds the directory it will not archive these files and remove from fl_list. $ cat fl_list /apps/dev/ihub/ready/IA003B/IA003B_Deal_Header_yyyymmdd_hhmmss.txt... (1 Reply)
Discussion started by: k9cheung
1 Replies

8. Shell Programming and Scripting

Script to Edit the file content and create new file

I have a requirement, which is as follows *. Folder contains list of xmls. Script has to create new xml files by copying the existing one and renaming it by appending "_pre.xml" at the end. *. Each file has multiple <Name>fileName</Name> entry. The script has to find the first occurance of... (1 Reply)
Discussion started by: sudesh.ach
1 Replies

9. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

10. Shell Programming and Scripting

shell script to edit the content of a file

Hi I need some help using shell script to edit a file. My original file has the following format: /txt/email/myemail.txt /txt/email/myemail2.txt /pdf/email/myemail.pdf /pdf/email/myemail2.pdf /doc/email/myemail.doc /doc/email/myemail2.doc I need to read each line. If the path is... (3 Replies)
Discussion started by: tiger99
3 Replies
Login or Register to Ask a Question