I am a real beginner in scripting, so I am struggling with a really easy task!
I want to write a script to concatenate several text files onto each other and generate a new file. I wanted the first argument to be the name of the new file, so:
would concatenate file1-3 and generate a file new_file.txt
Here is what I did so far:
But the problem is, it generates the file but the echo $name does not write anything into my file and I don't know what is the problem here :S
Last edited by Don Cragun; 08-16-2014 at 01:40 PM..
Reason: Add missing CODE tags.
Redirection is your friend. Consider cat as well, not echo. This is the heart of the logic you need:
You can make a loop, you do not have to check if a parameter is zero length unless the parameter is "" but if you require file checking:
Or make a one-liner using:
The cat command is short for concatenate, meaning to string together. So cat is what I would use to do what you ask. This does not check for existence of the input file or a missing or bad filename.
None of the above is checking return codes, the $? variable which you should check in case you have an error other than bad file name. Like permissions.
Last edited by jim mcnamara; 08-16-2014 at 09:37 AM..
I am a real beginner in scripting, so I am struggling with a really easy task!
I want to write a script to concatenate several text files onto each other and generate a new file. I wanted the first argument to be the name of the new file, so:
would concatenate file1-3 and generate a file new_file.txt
Here is what I did so far:
But the problem is, it generates the file but the echo $name does not write anything into my file and I don't know what is the problem here :S
First a few comments on the above code. The commands marked in red seem strange, dangerous, or wrong and dangerous.
In the first case, if any of your command line arguments contain whitespace, you won't get what you want. Use:
instead.
In the second case, you set a variable named filename and then print the contents of a variable named name. The 1st time through the loop, name is definitely not set. On subsequent times through the loop, name is still not set as long as you're using bash. (If you were using ksh it would be defined, but probably wouldn't be what you want. See later comments.) This is why, this echo prints nothing (but it would not affect what actually gets written into your destination file).
The third line marked in red will do what you want as long as there are no whitespace characters in any of the names of the files you're processing. You should ALWAYS quote strings that you get from a user to protect against there being a <space>, <tab>, or <newline> where you wouldn't expect one.
The fourth line marked in red could do some very strange things if there are any whitespace characters (other than <newline>) in the current line in your file. If the first character on a line is a <space> or a <tab> name will be set to an empty string as the rest of the line will be interpreted as a command to be executed with name set to an empty string. I presume what you wanted here was:
but I have no idea why you aren't just using line instead of making a copy of $line in name.
Furthermore, all expansions of the variables $i, $filename, and $name should be quoted.
Now for the fun part. Why didn't you tell us that in addition to creating new_file.txt your script also created file1.txt.txt, file2.txt.txt, and file3.txt.txt. The then side of your first if statement saves the name of the file you want to use as your destination file in the variable filename. The else side of that if statement replaces the destination saved in filename with the name of the current source file!!! There is nothing in the above script that writes anything to $1.txt. The touch command just updates the timestamp of that file if it already existed or creates it if it didn't exist; it doesn't alter its contents.
But, none of the above explains what you say has been happening with all of the suggested code that others have provided you in this thread. Have you verified that your early testing didn't empty out your source files? Please show us the output of the commands:
This will tell us if my guess about creating file*.txt.txt is correct, tell us if there might be an permission problems, and lets us verify that your input files aren't empty.
Have any of the commands that have been suggested produced any diagnostic messages?
You said you want to concatenate files, but it looks like your code is trying to remove empty lines, remove blank lines, or maybe remove lines starting with any whitespace characters and maybe to remove all trailing whitespace from every line that is in the source files.
Please give us a precise definition of exactly what you want this script to do!
If you are just trying to create a file with the name specified by the 1st operand (with .txt added if the name didn't already end with that string) and concatenate all of the files named by the remaining operands into it, a much simpler solution would be something like:
If you're trying to delete empty lines while copying data to the destination file, change the last line to:
I used the Korn shell, but this will also work with bash or any shell that performs the basic variable expansions required by the POSIX standards. (Of course this script should verify that it was invoked with at least two operands before trying the cat or grep, but I'll leave that as an exercise for the reader.
This User Gave Thanks to Don Cragun For This Post:
Hi All
I am a new in scripting language and I would like help for you guys
I would like to create a file named constant.h and search into all files *.m in specific directory for a reg-exp @"LBL_]+" exp: @"LBL_75847" , and write those matchs to constant.h if there are not written (no... (15 Replies)
I am suppose to make a program that cuts the extension off of a file. It uses that .ext to decide which program needs to be used to open the file. This only finds the name of the program from a list of programs I made. My problem is when it has the name of the program it needs to use, how can I... (2 Replies)
I am dealing will many thousand fairy small files.
I need to search them for various matches and depending on what I find, may need to search some files again for additional matches.
Generally speaking, is it better to write a txt file to an @array/@list and then work with it (multiple... (1 Reply)
How do I find all "regular" files on solaris(8) that are open for write ( +read as well).
I tried using pfiles, and lsof commands, but not sure how to get exactly what I wanted.
ps -e | awk '{ print $1 }' | xargs -i pfiles {} 2>/dev/null (10 Replies)
I'm a total *nix newb so I really could use some help! :o
I plan on running a cron job on my server that deletes some files and then backs up and emails the website for archival purposes.
Before I get in too deep I'd like to remove some unnecessary files that phpThumb creates. I'm not sure... (4 Replies)
Hi,
i have 600 or more file on my server and from this 600 files i want to make one file. i have written this script
SPATH=/etlstg/DAT
TPATH=/etlstg/DAT/DAT_PROCESSED
FILE_DATE=`TZ=CST+24 date +20%y%m%d`
a1=0
while
do
a2=0
while
do
a3=0
while
... (1 Reply)
Hi-
I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1
cn=bob,cn=user,dc=com
cn=kev,cn=user,dc=com
cn=john,cn=user,dc=com
I have a second file e.g. file.template which looks something like :-
dn: <dn>
objectclass:... (5 Replies)
Hi all,
i am new to unix,i have never worked on scripting and all,i am learning now and i have to write a script to compare two files.
The requirement is like : in the first file i am searching for a word and after i get that word i have to select everything from the rest of the file and redirect... (34 Replies)
Hello,
I am a Newbie in ksh Unix Script.
So I must write a ksh/sh script who read character at a position in a File. So also it must read all the lines who belongs at these characters , then write these lines in a another File.
Can you help me , or give little councils to advance with my... (5 Replies)
I'm trying to do two different things (converting an OpenVms .com to a ksh shell script): 1) open/read/err= 2) open/write/err=
Any help? I've found some things, but can't seem to find the correct way. (1 Reply)