I have research some days and found that we can add a file with some contents to the zip file and then compress it again. then the comments will be added to the zip file, but i don't know what that file exactly is, so any one know the principle for adding comments to a zip(compressed) file
The -c parameter will let you add a one-line comment for each file in the zip interactively. E.g. zip -c valid.zip somefile.txt
The -z parameter will let you add a multi-line comment for the entire zip archive interactively. E.g. zip -z valid.zip somefile.txt
For command-line use, feed the per-file comments (-c) and/or the per-zip comment (-z) to stdin. When using both, feed in the per-file comments first (one line each), followed by the per-zip comment line (s). Example:
$ touch one.txt two.txt
$ zip -c -z commented.zip one.txt two.txt <<END
> Comment for one
> Comment for two
> Remaining lines are zip-level comment
> lines. There can be more than one line here.
> End with a dot:
> .
> END
adding: one.txt (stored 0%)
adding: two.txt (stored 0%)
Enter comment for one.txt:
Enter comment for two.txt:
enter new zip file comment (end with .):
$ unzip -l commented.zip
Archive: commented.zip
Remaining lines are zip-level comment
lines. There can be more than one line here.
End with a dot:
Length Date Time Name
--------- ---------- ----- ----
0 11-13-2019 10:06 one.txt
Comment for one
0 11-13-2019 10:06 two.txt
Comment for two
--------- -------
0 2 files
Read the manual for your version of zip. On the Macintosh, the -c parameter will allow you to add a comment line.
Related
How do I copy lines from one file to another in Linux without opening source and destination files and I need to exclude the comments when copying the lines.
I do not want to copy the comments in the first file and the files are in different locations
Assuming lines are commented with # at the very beginning of each line, the following should work:
grep -v "^#" path/to/input/file >path/to/output/file
(Note: This will either create a new output file or irreversibly overwrite the output file if it already exists)
Assuming comment lines in your file contain # at the beginning of each line, the following sed command will delete these lines:
$ sed '/^#/d' path/to/input-file > path/to/output-file
If your file can also contain lines with whitespace before the #, the following sed command will delete lines beginning with zero or more spaces or tabs (in any order), followed by a hash (#) character:
$ sed '/^[ \t]*#/d' path/to/input-file > path/to/output-file
If your file also contains lines containing code followed by a comment, the following sed command should work:
$ sed -e '/^[ \t]*#/d' -e 's/#.*$//' path/to/input-file > path/to/output-file
I need to copy only the last line of a lot of files to another file. How can I do that? Please help me.
I know tail to take the last file and > to put that to other file but I can do the same thing to a lot of files?
Try:
tail -qn 1 inputfile1 inputfile2 ... > outputfile
-n 1 for outputting only the last line, -q for suppressing the header.
See:
man tail
I have two files data.txt and results.txt, assuming there are 5 lines in data.txt, I want to copy all these lines and paste them in file results.txt starting from the line number 4.
Here is a sample below:
Data.txt file:
stack
ping
dns
ip
remote
Results.txt file:
# here are some text
# please do not edit these lines
# blah blah..
this is the 4th line that data should go on.
I've tried sed with various combinations but I couldn't make it work, I'm not sure if it fit for that purpose as well.
sed -n '4p' /path/to/file/data.txt > /path/to/file/results.txt
The above code copies line 4 only. That isn't what I'm trying to achieve. As I said above, I need to copy all lines from data.txt and paste them in results.txt but it has to start from line 4 without modifying or overriding the first 3 lines.
Any help is greatly appreciated.
EDIT:
I want to override the copied data starting from line number 4 in
the file results.txt. So, I want to leave the first 3 lines without
modifications and override the rest of the file with the data copied
from data.txt file.
Here's a way that works well from cron. Less chance of losing data or corrupting the file:
# preserve first lines of results
head -3 results.txt > results.TMP
# append new data
cat data.txt >> results.TMP
# rename output file atomically in case of system crash
mv results.TMP results.txt
You can use process substitution to give cat a fifo which it will be able to read from :
cat <(head -3 result.txt) data.txt > result.txt
head -n 3 /path/to/file/results.txt > /path/to/file/results.txt
cat /path/to/file/data.txt >> /path/to/file/results.txt
if you can use awk:
awk 'NR!=FNR || NR<4' Result.txt Data.txt
I have a file called flw.py and would like to write a bash script that will replace some text in the file (take out the last two lines and add two new lines). I apologize if this seems like a stupid question. A thorough explanation would be appreciated since I am still learning to script. Thanks!
head -n -2 flw.py > tmp # (1)
echo "your first new line here..." >> tmp # (2)
echo "your second new line here...." >> tmp #
mv tmp flw.py # (3)
Explanation:
head normally prints out the first ten lines of a file. The -n argument can change the number of lines printed out. So if you wanted to print out the first 15 lines you would use head -n 15. If you give negative numbers to head it means the opposite: print out all lines but the last N lines. Which happens to be what you want: head -n -2
Then we redirect the output of our head command to a temporary file named tmp. > does the redirecting magic here. tmp now contains everything of flw.py but the last two lines.
Next we add the two new lines by using the echo command. We append the output of the echo "your first new line here..." to our tmp file. >> appends to an existing file, whereas > will overwrite an existing file.
We do the same thing for the second line we want to append.
Last, we move the tmp file to flw.py and the job is done.
You can use single sed command to get you expect result
sed -n 'N;$!P;$!D;a\line\n\line2' fly.py
Example:
cat fly.py
1
2
3
4
5
sed -n 'N;$!P;$!D;a\line\n\line2' fly.py
Output :
1
2
3
line1
line2
Note :
Using -i option to update your file
Original file contains:
B
RBWBW
RWRWWRBWWWBRBWRWWBWWB
My file contains :
B
RBWBW
RWRWWRBWWWBRBWRWWBWWB
However when i use the command diff original myfile it shows following:
1,3c1,3
< B
< RBWBW
< RWRWWRBWWWBRBWRWWBWWB
---
> B
> RBWBW
> RWRWWRBWWWBRBWRWWBWWB
When i put -w tag (diff original myfile -w) it shows no differences... but I'm absolutely sure these two files do not have whitespace/endline differences. What's the problem?
These texts are equal.
Maybe you have extra white spaces.
try
diff -w -B file1.txt file2.txt
-w Ignore all white space.
-B Ignore changes whose lines are all blank.
As seen in the comments, you must have some different line endings, caused because of an original file coming from a DOS system. That's why using -w dropped the end of the line and files matched.
To repair the file, execute:
dos2unix file
Look at them in Hex format. This way you can really see if they are the same.