Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 21 Dec 2014, 17:10
All times are UTC - 4
 Forum index » House Training » Beginners Help ( Start Here)
Need script to modify or combine text files
Moderators: Flash, Ian, JohnMurga
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
Cur Dog

Joined: 22 Dec 2007
Posts: 60

PostPosted: Mon 14 Apr 2008, 00:06    Post_subject:  Need script to modify or combine text files  

Can someone help point me to a guide that would help me to write a script that would either combine two text files or add lines of text from within the script file itself to an existing text file.

Thanks,
Cur Dog
Back to top
View user's profile Send_private_message 
Bruce B


Joined: 18 May 2005
Posts: 11131
Location: The Peoples Republic of California

PostPosted: Mon 14 Apr 2008, 00:27    Post_subject:  

combining two text files is usually appending one to another.

say we want to append file2.txt to file1.txt

# cat file2.txt >>file1.txt

if you planned to do the same action over and over then the line in the script read the same way.

if you only use one > you will clobber file1.txt, so be advised

if we want a script to append it's a wonderful day to a file it could like this:

Code:
#!/bin/sh
if [ ! $1 ] ; then
  echo please enter a filename
  exit
fi

echo "it's a wonderful day">>$1
Back to top
View user's profile Send_private_message 
Cur Dog

Joined: 22 Dec 2007
Posts: 60

PostPosted: Mon 14 Apr 2008, 18:29    Post_subject:  

OK, I must be missing something......This is what I used.

Code:

#!/bin/sh
if [ ! $1 ] ; then
 echo  /root/my-documents/Scripts/Display
  exit
fi

echo "it's a wonderful day">>$1


Display is a text file that has on line of text in it.
"Test File"
After I execute the script I was expecting to see....
"Test File"
"it's a wonderful day"

Are my expectations incorrect?

Bruce: Thanks for the reply!!!!
Back to top
View user's profile Send_private_message 
Juveno41

Joined: 09 Apr 2008
Posts: 25

PostPosted: Mon 14 Apr 2008, 19:00    Post_subject:  

Some programs seem to need special variables or lines set in a system file.

Let's say, we have to add a line in the file '/root/.xinitrc' after installing a PET package.

for example :

export VARIABLE_NAME="VARIABLE_VALUE"


then you can't just append a line to '/root/.xinitrc',as it would cause some problems.


How do people handle this?

Should this be done with a script? If so, can someone tell me where I can see some examples, please.

Or is it better not to mess up with system files in this manner...
Back to top
View user's profile Send_private_message 
Pizzasgood


Joined: 04 May 2005
Posts: 6270
Location: Knoxville, TN, USA

PostPosted: Mon 14 Apr 2008, 19:23    Post_subject:  

You mis-read his example. It was supposed to be used exactly, with no modifications. The part you changed was to tell the user they were suppposed to call the program with a parameter:
thescriptsname "/root/blah.txt"
Then, it would append "it's a wonderful day" to the file /root/blah.txt.

This will append "eat more cheese" to /tmp/todolist:
echo "eat more cheese" >> /tmp/todolist

This will append the contents of /tmp/stuff to /tmp/bob
cat /tmp/stuff >> /tmp/bob

Basically, if you put >> /tmp/file after any command, that command's normal output will be appended to /tmp/file. You can append the error output like this:
2>> /tmp/file
You can append both normal and error output to /tmp/file like this:
>>/tmp/file 2>&1

The echo command simply outputs it's input, which is why you can use it to output specific things, or the contents of a specific variable:
echo $MYVAR >> /tmp/stuff

The cat command displays the content of the file you use it on. cat /tmp/stuff just displays the contents of /tmp/stuff. Then adding the >> /tmp/bob appends that output to /tmp/bob.

_________________
Between depriving a man of one hour from his life and depriving him of his life there exists only a difference of degree. --Muad'Dib

Back to top
View user's profile Send_private_message Visit_website 
Cur Dog

Joined: 22 Dec 2007
Posts: 60

PostPosted: Mon 14 Apr 2008, 22:27    Post_subject:  

Well I guess I am having problems seeing the forest because of all of the trees. I still can't get the example to work. If I use the cat command in the script, it appends one file to the other. This is actually the way that I believe benefits me the most. However, I still would like to understand what I am doing wrong. I have saved the example as first.sh. If I execute it nothing happens. I have called it from within another script
Code:

first.sh"/root/my-documents/Scripts/Display"

I know that I must be making this harder than it really is, please forgive my ignorance.
Back to top
View user's profile Send_private_message 
Bruce B


Joined: 18 May 2005
Posts: 11131
Location: The Peoples Republic of California

PostPosted: Tue 15 Apr 2008, 00:03    Post_subject:  

Code:

first.sh"/root/my-documents/Scripts/Display"


1. We generally start our bash scripts with this line as line one:

#/bin/sh

then the commands

2. first.sh"/root/my-documents/Scripts/Display"

would fail because there is no such command, the major problem is the missing space see below:

first.sh "/root/my-documents/Scripts/Display"

with a space first.sh will use "/root/my-documents/Scripts/Display" as its first argument which is AKA $1

presuming first.sh is supposed to do something to "/root/my-documents/Scripts/Display" then it will

3. just a minor comment

"/root/my-documents/Scripts/Display"

Linux is a case sensitive OS, sometimes it's helpful to mix case in file names and other times a hindrance. everything equal - linux people prefer to do lower_case_filenames and avoid spaces in the file-name, using underscores and dashes as illustrated to make the name easier to read.

Spaces can be used, Linux can handle them, but not all utilities and programs written for Linux handles them properly.

When spaces are used the filename must be quoted or escaped another way. You quoted and that's fine, but not necessary - the reason why is there are no spaces in the filename.

So:

this:

/root/my-documents/Scripts/Display

is as good as this:

"/root/my-documents/Scripts/Display"

-------------------------

That sums up all the error and trivia I see from your post and then some.
Back to top
View user's profile Send_private_message 
Cur Dog

Joined: 22 Dec 2007
Posts: 60

PostPosted: Tue 15 Apr 2008, 00:37    Post_subject:  

I would like to say THANK YOU to both Bruce and Pizzasgood. It was the missing space that was causing my problem. That can make one feel really stupid.

Cur Dog
Back to top
View user's profile Send_private_message 
Bruce B


Joined: 18 May 2005
Posts: 11131
Location: The Peoples Republic of California

PostPosted: Tue 15 Apr 2008, 16:08    Post_subject:  

Stupid is giving up. Intelligence is recognizing when you need help and you get help.

I'll recommend Steve Parker's Tutorial as a good one to get started with if you want to learn more about bash scripting.
Back to top
View user's profile Send_private_message 
Cur Dog

Joined: 22 Dec 2007
Posts: 60

PostPosted: Tue 15 Apr 2008, 23:45    Post_subject:  

Thanks, I will be sure to check it out.
Again, I thank you for your help.

Cur Dog
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » House Training » Beginners Help ( Start Here)
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0704s ][ Queries: 11 (0.0030s) ][ GZIP on ]