Need script to modify or combine text files

Booting, installing, newbie
Post Reply
Message
Author
Cur Dog
Posts: 64
Joined: Sun 23 Dec 2007, 00:49

Need script to modify or combine text files

#1 Post by Cur Dog »

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

Bruce B

#2 Post by Bruce B »

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: Select all

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

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

Cur Dog
Posts: 64
Joined: Sun 23 Dec 2007, 00:49

#3 Post by Cur Dog »

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

Code: Select all

#!/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!!!!

Juveno41
Posts: 25
Joined: Wed 09 Apr 2008, 12:35

#4 Post by Juveno41 »

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...

User avatar
Pizzasgood
Posts: 6183
Joined: Wed 04 May 2005, 20:28
Location: Knoxville, TN, USA

#5 Post by Pizzasgood »

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.
[size=75]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[/size]
[img]http://www.browserloadofcoolness.com/sig.png[/img]

Cur Dog
Posts: 64
Joined: Sun 23 Dec 2007, 00:49

#6 Post by Cur Dog »

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: Select all

first.sh"/root/my-documents/Scripts/Display"
I know that I must be making this harder than it really is, please forgive my ignorance.

Bruce B

#7 Post by Bruce B »

Code: Select all

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.

Cur Dog
Posts: 64
Joined: Sun 23 Dec 2007, 00:49

#8 Post by Cur Dog »

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

Bruce B

#9 Post by Bruce B »

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.

Cur Dog
Posts: 64
Joined: Sun 23 Dec 2007, 00:49

#10 Post by Cur Dog »

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

Cur Dog

Post Reply