adding text to a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding text to a file
# 1  
Old 10-23-2009
adding text to a file

Hi All

I am making a script in which.I have a problem i try to discribe.

Code:
File1 content need to be append in file 2 but such as if content already exist then dont add.

File1 IS Like this

myname
yourname
hername
hisname

File2 

%AddNameHere  myname yourname hername hisname

# 2  
Old 10-23-2009
Is this what you looking for ?

The file2 layout that you show does not reflect what you stated as "append" the name to the file, but I'm assuming append (meaning at the end of the file). I'm also assuming that each name is on a separate line in file2. Considering those assumptions, here is what I came up with for an example in bash:

Code:
#!/bin/bash

#
#       check arguments
#
if (( $# != 2 ))
then
        echo ""
        echo $0 : Invalid number of arguments $#
        echo ""
        exit 1
fi

#
#       declare variables (easier read)
#
declare file1=$1
declare file2=$2

#
#       read each line in file1, check if it exists in
#       file2, and if not, then append it.
#
cat $file1 | \
while read name
do
        egrep -s $name $file2
        if (( $? != 0 ))
        then
                echo $name >> $file2
                echo "added name "$name
        fi
done

let me know if this does it, or if my assumptions are wrong.
# 3  
Old 10-23-2009
Quote:
Originally Posted by sdanner
The file2 layout that you show does not reflect what you stated as "append" the name to the file, but I'm assuming append (meaning at the end of the file). I'm also assuming that each name is on a separate line in file2. Considering those assumptions, here is what I came up with for an example in bash:

Code:
#!/bin/bash

#
#       check arguments
#
if (( $# != 2 ))


Non-standard. Use:

Code:
if [ $# -ne 2 ]

Quote:
Code:
then
        echo ""
        echo $0 : Invalid number of arguments $#
        echo ""
        exit 1
fi

#
#       declare variables (easier read)
#
declare file1=$1
declare file2=$2


declare is non-standard and unnecessary.
Quote:
Code:
#
#       read each line in file1, check if it exists in
#       file2, and if not, then append it.
#
cat $file1 | \


UUOC and the backslash is unnecessary.
Quote:
Code:
while read name
do
        egrep -s $name $file2
        if (( $? != 0 ))


See above.
Quote:
Code:
        then
                echo $name >> $file2
                echo "added name "$name
        fi
done

let me know if this does it, or if my assumptions are wrong.
# 4  
Old 10-23-2009
corrected script, corrections made per C. Johnson

To: cfajohnson... thanks for taking the time to fix this up. I'll try and keep your advice in mind next time. ( Guess I should get your book, huh? :>D )

To: aliahsan81... just for completeness, here is the same script with cfajohnson suggestions.

Code:
#!/bin/bash

#
#       check arguments
#
if [ $# -ne 2 ]
then
        echo ""
        echo $0 : Invalid number of arguments $#
        echo ""
        exit 1
fi

#
#       declare variables (easier read)
#
file1=$1
file2=$2

#
#       read each line in file1, check if it exists in
#       file2, and if not, then append it.
#
while read name
do
        egrep -s $name $file2
        if [ $? != 0 ]
        then
                echo $name >> $file2
                echo "added name "$name
        fi
done < $file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

About adding users from a text file using bash

hello, I try to add users from a text file with this form: username:groupename:homedir first i extract data which is separated by ":" then i use useradd to add users/groups. but,,, my code doesn't works : #!/bin/bash echo "give me a text file: " read dir # control if... (2 Replies)
Discussion started by: ref012
2 Replies

2. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

3. Shell Programming and Scripting

adding a line to a text file

I have a tab delimited text file, id name distance 1 3325167 0.334561754018 2 3290488 0.389444269458 3 3288794 0.392312701782 4 3347602 0.392532202097 5 3295355 0.394394169485 I need to add a line after the header line. The first and third field of... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

Adding text into file

Hello, I after some assistance please. Below is a file I need to read and for each line write an output. My input file looks like this 2 20008 2003-08-26 2 20032 2003-08-26 2 20041 2003-08-26 2 20037 2003-08-26 2 20050 2003-08-26 6 ... (6 Replies)
Discussion started by: giles.cardew
6 Replies

5. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

6. Shell Programming and Scripting

Adding Text To Middle Of File

Hi I am trying to write a bash script to add a line of text to the middle of a file. The way I worked it out was to calculate the number of lines and divide by 2. The file I am using is called newfile and it just contains lines of data. The new file I have created I have called midfile and... (7 Replies)
Discussion started by: BundBash
7 Replies

7. Shell Programming and Scripting

Adding a delimiter to a text file

Im writing a KSH script to read a simple text file and add a delimiter. Ive written the following script but it runs very slow. I initially used the cut command to substring the input record then switched to this version using awk to substring... both run too slow. Any ideas how to make this more... (2 Replies)
Discussion started by: lock
2 Replies

8. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

9. Shell Programming and Scripting

adding text to a file between lines

Suppose content of my first file: first line second line third line How can i insert text between "first line" & "second Iline" Any help?????/ (7 Replies)
Discussion started by: bishweshwar
7 Replies

10. Shell Programming and Scripting

Adding Text To each line of a file

How would I add text to the beginning of each line in a text file in a script right after the file is created from another text file. (4 Replies)
Discussion started by: cubs0729
4 Replies
Login or Register to Ask a Question