I want to combine multiple text files into one text file. Is there any command in ubuntu terminal to do this, or do I have to do it manually?
Try cat like
cat file1 file2 file3 > outputFile
cat stands for concatenation.
> is for output redirection.
If outputFile already has something in it and you wish to append to it the contents of other files, use
cat file1 file2 file3 >> outputFile
as > would erase the old contents of outputFile if it already existed.
Have a look here as well.
Related
I know minizip can do this,but that's too complex. And for some reason i can only include zlib.h.
I am try to do this based on zpipe.c. I expect to output a .z or .gz that contains different files (include directory is not necessary).
My way is
deflateInit()->deflateSetHeader()->fread()->deflate()->fwrite()->deflateEnd()
, then do it again by open output.z again in an additional way.The result is that 2 files be compressed to one file in the output.z, that may not be what i want.
Is it possible to compress more than one file with a few lines of code?
You can concatenate the compressed outputs in your zpipe.c based code.
Do not fclose() the output file. Keep appending to it.
gunzip of the concatenated file is equivalent to cat file1 file2. If you
want to recover the multiple files will need to uncat the output file yourself
in your zpipe.c based code. Basically, continue fread() even though inflate()
returned you a Z_STREAM_END.
Here is a proof that concatenating works
$ echo "this is file 1" > file1
$ echo "this is file 2" > file2
$ gzip file1
$ gzip file2
$ cat file1.gz file2.gz > files.gz
$ rm file1.gz file2.gz
rm: remove regular file 'file1.gz'? y
rm: remove regular file 'file2.gz'? y
$ gunzip files.gz
$ cat files
this is file 1
this is file 2
I'm a very new user to bash, so bear with me.
I'm trying to run a bash script that will take inputs from the command line, and then run a c program which its output to other c programs.
For example, at command line I would enter as follows:
$ ./script.sh -flag file1 < file2
The within the script I would have:
./c_program -flag file1 < file2 | other_c_program
The problem is, -flag, file1 and file2 need to be variable.
Now I know that for -flag and file this is fairly simple- I can do
FLAG=$1
FILE1=$2
./c_program $FLAG FILE1
My problem is: is there a way to assign a variable within the script to file2?
EDIT:
It's a requirement of the program that the script is called as
$ ./script.sh -flag file1 < file2
You can simply run ./c_program exactly as you have it, and it will inherit stdin from the parent script. It will read from wherever its parent process is reading from.
FLAG=$1
FILE1=$2
./c_program "$FLAG" "$FILE1" | other_c_program # `< file2' is implicit
Also, it's a good idea to quote variable expansions. That way if $FILE1 contains whitespace or other tricky characters the script will still work.
There is no simple way to do what you are asking. This is because when you run this:
$ ./script.sh -flag file1 < file2
The shell which interprets the command will open file2 for reading and pipe its contents to script.sh. Your script will never know what the file's name was, therefore cannot store that name as a variable. However, you could invoke your script this way:
$ ./script.sh -flag file1 file2
Then it is quite straightforward--you already know how to get file1, and file2 is the same.
I want to create a windows batch file (Win7) to achieve the following:
Copy source.doc to destination with destinationFilename.doc taken from a list in a text file (nameList.txt)
I have batch file that will make directories from nameList.txt but I can't figure out how to modify the batch file to make it copy source.doc in the required manner.
Using xargs you can process a file nameList.txt which contains a newline separated list of target filenames like this:
cat nameList.txt | xargs -I "F" cp source.doc F
where -I "F" defines F as a placeholder to be used in command invocation of cp.
I am not sure what I am doing wrong in following situation - I want to merge multiple files into one, but I want to merge a lot of them, so I want to use something like this:
*input file* that contains lines
file1.txt >> mergedfiles1.txt
file2.txt >> mergedfiles1.txt
file3.txt >> mergedfiles2.txt
file4.txt >> mergedfiles2.txt
...
If I try to use simple script as I usually do
cat *input file* | while read i
do
cat $i
done
it actually doesn't merge the files, but writes
*content* of file1.txt
cat: >> : file does not exist
cat: mergedfiles1.txt : file does not exist
I have tried to put command cat right at the beginning of each line of the input file but it did not work as well.
I guess it is a simple mistake, but I am not able to find a solution.
Thanks for help.
You can merge your three files using cat this way:
cat file1 file2 filet3 > merged_files
you need to use it like this
cat input_file > output file
That's because bash treats an empty space as a line separator.
So you've got to manage this thing.
Actually, you can remove the >> from your input file and do something like this :
k=0
for line in $(cat inputFile); do
if [ $k -eq 0 ]; then
src=$line
let k=$k+1
else
cat $src>>$line
k=0
fi
done
It's been a while since my last bash script, but the logic is pretty simple.
Since bash uses the spaces a line separators, you have to keep a counter to know if the line's really over.
So we're using k.
Having k = 0 means that we're in the first half of the line, so we need to store the filename into a var (src).
When k is 1, it means that we're in the final half of our line, so we can actually execute the cat command.
My code will work if your text is like :
file1.txt mergedfiles1.txt
file2.txt mergedfiles1.txt
file3.txt mergedfiles2.txt
file4.txt mergedfiles2.txt
I wasn't quite sure what you wanted to do, but thought this example might help:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for file in *
do
echo "$file"
done
IFS=$SAVEIFS
The manipulating of IFS is so you can pick up and process space separated files, which are more common coming out of Windows reports than -- at least from my experience -- Linux (Unix).
I've been told I can concatenate files in a sort of batch file by using sh.
e.g.
cat file1 file2 >> file3
cat file1 file4 >> file5
Is it possible to do a string replacement, I mean load file, replace x with y and run the file with sh.
The 'sed' utility, the stream editor, is the shell tool to do this. You could do something like the following to do what you asked, join file1 and file2, change all occurances of x to y and then run the commands with the bash shell:
cat file1 file2 | sed -e 's/x/y/g' | bash