How to add text in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add text in a file
# 1  
Old 10-20-2008
How to add text in a file

I want to edit my /etc/security/user, But they are so many.
For example for my user : a1,a2,a3
i want to add this text.

loginretries = 0
minalpha = 0
minlen = 0
minage = 0

For example.
a1:
admin = false

sysad:
login = false
rlogin = false
admin = false
account_locked = true
loginretries = 5

a2:
admin = false

a3:
admin false


OUTPUT:

will look like this


a1:
admin = false
loginretries = 0
minalpha = 0
minlen = 0
minage = 0


sysad:
login = false
rlogin = false
admin = false
account_locked = true
loginretries = 5

a2:
admin = false
loginretries = 0
minalpha = 0
minlen = 0
minage = 0


a3:
admin false
loginretries = 0
minalpha = 0
minlen = 0
minage = 0
# 2  
Old 10-20-2008
Try this:

Code:
awk '
BEGIN{t="loginretries = 0\nminalpha = 0\nminlen = 0\nminage = 0"}
/^a1:/||/^a2:/||/^a3:/{print;getline;print $0 "\n" t;next}
1' file > newfile

Regards

Last edited by Franklin52; 10-21-2008 at 04:32 AM.. Reason: correction code
# 3  
Old 10-20-2008
awk '
BEGIN{t="loginretries = 0\nminalpha = 0\nminlen = 0\nminage = 0"}
/^a1:/||/^a2:/||/^a3:/{print;getline;print $0 "\n" t}
' file > newfile

what if i wont declare them on that statement. there are lot of accouts? I want to put it in a file. like

$cat list1.txt
a1
a2
a3

How to do it?
# 4  
Old 10-21-2008
Code:
awk '
BEGIN{t="loginretries = 0\nminalpha = 0\nminlen = 0\nminage = 0"}
NR==FNR{a[$0]=$0;next}
{s=$0;sub(":","",s)}
a[s]{print;getline;print $0 "\n" t;next}
1' list1.txt /etc/security/user

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

2. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

3. UNIX for Dummies Questions & Answers

Add a word to Text file

Hello, I have a mysql text file. I want add a word to it. Thanks for help. Sample text: ,'address','166 Warren Street, NY 12534'),(45215,26556,'phone','(518)811-4145'),(151426,15565,'listing_duration' ,'address','233 Tan Street, CA... (1 Reply)
Discussion started by: hoo
1 Replies

4. Shell Programming and Scripting

Add a word to Text file

Hello, I have a mysql text file. I want add word to it. Thanks for help. Sample sql file: ,'address','166 Warren Street, NY 12534'),(45215,26556,'phone','(518)811-4145'),(151426,15565,'listing_duration','address','122 Hom Street, NY... (6 Replies)
Discussion started by: hoo
6 Replies

5. Shell Programming and Scripting

how to add text into the last line of text file

I need help with insert text to the last line of text file with echo command I know can do something like echo "i4\n$logtext\n.\nwq" | ex -s $file can insert to first line, but how can i change this code in order to insert to the last line of text file? please help, thank you :( (2 Replies)
Discussion started by: gavin_L
2 Replies

6. Shell Programming and Scripting

find a string in a file and add some text after that file

Hi Could you please help me out by solving teh below problem ? I have a file with as below source1|target1|yes source2|target2|no source1 is file in which i have to place some code under the <head> tag in it. What code i have to place in source1 is something like this "abcd.....<target1>... (5 Replies)
Discussion started by: Tasha_T
5 Replies

7. Shell Programming and Scripting

Add new column in Text File

I have one text file which is result of bdf command that have 6 fields separated by space and I want to add one new column in the beginning which is the name of the server because I have to insert whole thing into oracle table consisting of 7 fields THis is not a complete list but it looks... (9 Replies)
Discussion started by: pareshan
9 Replies

8. Shell Programming and Scripting

how to add text to the text file

i want to combine 2 user inputs and add to the last line of text file for an example : enter input1 enter input2 then i want input1 input2 add to the last line of text file anyone help me? (8 Replies)
Discussion started by: CheeSen
8 Replies

9. Shell Programming and Scripting

add text in the middle of file

Can anyone help me pls? I want to add a text into the middle of file. I've writtenthe following script text to add="$1" file="$2" lines=$(wc -l $2) half_lines=$(expr $lines / 2) head -$half_lines $2 > temp echo "text to add" >> temp ((half_lines=$half_lines + 1)) tail -$half_lines $2... (6 Replies)
Discussion started by: relle
6 Replies

10. Shell Programming and Scripting

add text to end of text file

Hi, I assume there is a simple solution, but as usual i can't find it! How can i add a line of text to the end of a text file on a new line? i.e file.txt ________________ this is my text file ________________ file.txt ________________ this is my text file WITH A NEW LINE... (6 Replies)
Discussion started by: leeRoberts2007
6 Replies
Login or Register to Ask a Question