Need script to modify or combine text files
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
Thanks,
Cur Dog
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:
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
OK, I must be missing something......This is what I used.
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!!!!
Code: Select all
#!/bin/sh
if [ ! $1 ] ; then
echo /root/my-documents/Scripts/Display
exit
fi
echo "it's a wonderful day">>$1
"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!!!!
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...
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...
- Pizzasgood
- Posts: 6183
- Joined: Wed 04 May 2005, 20:28
- Location: Knoxville, TN, USA
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.
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]
[img]http://www.browserloadofcoolness.com/sig.png[/img]
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
I know that I must be making this harder than it really is, please forgive my ignorance.
Code: Select all
first.sh"/root/my-documents/Scripts/Display"
Code: Select all
first.sh"/root/my-documents/Scripts/Display"
#/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.
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.
I'll recommend Steve Parker's Tutorial as a good one to get started with if you want to learn more about bash scripting.