I have a flat file "xxx.txt"
It contains various records
aaaa
bbbb
cccc
...
......
etc
My script must create multiple flat files in the name of each of these records(number of records may vary).
Like aaaa.txt, bbbb.txt,.....,etc...
Also, the text files must contain a message in all those files created,
Example:
Hi,
Welcome
Thankyou
How to do this in shell script.
Note: In Unix SunOS 5.10(solaris)
Just use a loop:
#!/bin/bash
while IFS= read -r line; do
printf "Hi\nWelcome\nThank you\n" > "${line}.txt"
done < file
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 am using the following simple shell script to read transaction file against the contents of a master file , and just output the matching lines from the transaction file.
Transaction file contains:
this is a sample line - first
this is a sample line - second
this is a sample line - nth line
Master File contains:
first
Output:
this is a sample line - first
for tranfile in transactionfile.txt
do
grep -f MasterFile.txt $tranfile >> out.txt
done
PS: When i execute this line outside of the above shel script it works like a charm ; Just that it wont return within this shell script.
What am i missing???
Without the script output text, knowing what shell, i'm just guessing, but i suspect you either fail to find grep in the $PATH ( or using a different version of grep) or fail to find one of the files or you are executing a shell in the command line and another different shell in the script.
Try adding shebang in the script with the correct shell and try to put the grep path (usually /bin/grep or /usr/bin/grep) and also add the full path to the files you are requiring.
To help debug, i suggest you to add a set -x to the top of the script, so the shell will output what is doing and you can notice what is missing. This set -x may be replaced with a -x option in the shebang (example #!/bin/bash -x )
set -x also work in the command line, use set +x to disable it
I would have done this using awk
awk 'NR==FNR {a=$0;next} $0~a' master transaction
FNR==NR {a=$0;next} stores the data from master file in variable a
$0~a test every line of transaction if it contains variable a, if so do the default action print the line.
If master file contains more than one word. Test the last field against all words like this:
awk 'FNR==NR {a[$0];next} $NF in a' master transaction
If word can be anywhere on the line:
awk 'FNR==NR {a[$0];next} {for (i in a) if ($0~i) print}' master transaction
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 have file about 5.5GB of size. I want to view a particular line of the file. Lets say line number 100001 and I want to replace that line with my own texts. How to achieve this operation using Unix command. I can not view the file in editor. I can not be opened and that is a remote machine.
Can anyone share some idea to view that line and replacing it with some other texts?
Thanks :)
If you want to modify the line in-place and the replacement data is the same length as the text being replaced, you can use dd to (carefully!) overwrite part of the file.
# getting the byte offsets of the start and length of the line
perl -ne '$s+=length; if ($.==100001) {print "$s + ",length,"\n"; exit}' bigfile
# writing over the existing data
echo 'new line' | dd of=bigfile bs=1 seek=$start count=$length conv=notrunc
If the replacement data is a different length, and it's not at the very end of the file, you have no choice but to rewrite the file. This requires having enough disk space to keep both bigfile and a copy of it!
# The old file is renamed to bigfile.bak; a new bigfile is written with changes.
sed -i.bak -e '100001 c \
new line' bigfile