how can i delete part of the file with command line?
I have tried using sed the following way:
c:\sed '1,2!d' res.txt > res.txt
but the file became empty
what i exepect to get is
1 a
2 b
3 c
4 d
to become
1 a
2 b
in the same file res.txt
Add -i or --in-place switch to sed to read and write the same file. Also, windows command line uses double quotes. So you should use
sed -i "1,2!d" res.txt
Just try to call c:\sed '1,2!d' res.txt. You'll see correct result:
1 a
2 b
So, you can't use the same file for input and output. You can use different files and move/copy after it: c:\sed '1,2!d' res.txt > res.tmp & move /y res.tmp res.txt
Related
I need to remove first three lines of multiple markdown files structured like that:
|- Folder
| - 01-07-22.md
| - 02-07-22.md
| - 03-07-22.md
| - ...
I would like to do this with Mac Terminal (if possible) because I have no expertise with any coding language and thus I don't have any coding platform installed on my computer.
Also I would like to know if other than deleting first 3 lines is possible to add "##" at the very beginning of every document.
What works:
This work:
sed -i '' '1,3d' Folder/*.md
The following command works also:
sed -i '' '1i\
##' *.md
But it does not add a new line before the first line.
This does not work at all:
sed -i '' '1s
/^/##/' *.md
How to add an empty line at the beginning and "##" at the beginning of the now second line? Explaination:
From this:
# First line of example .md file
Second line of example .md
...
To this:
### First line of example .md file
Second line of example .md
...
You should be able to use (GNU) sed. I hope that the Mac version supports all the required flags and features.
To delete the first 3 lines:
sed -i '1,3d' Folder/*.md
To prepend the line ## to every file:
sed -i '1i##' Folder/*.md
To prefix the existing first line with ##:
sed -i '1s/^/##/' Folder/*.md
The original files are overwritten without confirmation, but you can specify -i.bak to create backup files, e.g. Folder/01-07-22.md.bak. Specify -i '' to disable backup file creation.
Certain sed implementations might always require an argument after -i, so go with -i.bak or -i .bak.
If prepending a line does not work, try a different syntax (the newline is important):
sed -i .bak '1i\
##' Folder/*.md
If that doesn't work either, there's another form how sed could be invoked:
sed -i .bak -e '1i\' -e '##' Folder/*.md
If you want to modify the first line and add an empty line before it, e.g. transforming
1
2
3
into
##1
2
3
would require you to use the following command:
sed -i .bak '1s/^/##/;1i\
' Folder/*.md
I just want to change one number inside a file then run it and save the output file. This need to be done in an iterative way.
cat file1
decay=( 1 5 2 6 )
for i in $decay
do
run file2.i
done
cat file2.i
cell 215 cell 115 cell ${i}
So now for each decay(i), I need file2.i to use the decay(i). Also, the output file will always have the same name file2.i_outp. How to save the output file each run instead of overwriting each i.
Copy the template file2.i to a temporary file, replacing ${decay} with the current array element.
decay=(1 5 2 6)
for i in $decay
do
sed "s/\\${i}/$i/" file2.i > /tmp/file2.i.$$
run /tmp/file2.i.$$
rm /tmp/file2.i.$$
done > file2.i_outp
This one worked fine
for i in {1 5 2 6}
do
sed "s/\\${i}/$i/" file2.i > /tmp/file2.i.$$
run /tmp/file2.i.$$
rm /tmp/file2.i.$$
done > file2.i_outp
Is it possible to insert heredoc contents directly to specific row in output file without temp file ?
cat <<-EOT > tmp.txt
some string
another string
and another one
EOT
sed -i '10 r tmp.txt' outputfile && rm tmp.txt
I've been using something like this , but I'd prefer to avoid needing tmp.txt.
ed might be a good choice
# create a test file
seq 15 > file
# save the heredoc contents in a variable
new=$(cat <<-EOT
some string
another string
and another one
EOT
)
# note the close parenthesis must **not** be on the same line as the heredoc word
# add the contents into the file
ed file <<EOF
10i
$new
.
wq
EOF
cat file
1
2
3
4
5
6
7
8
9
some string
another string
and another one
10
11
12
13
14
15
You could merge the two heredocs to save a step:
ed file <<-EOF
10i
some string
another string
and another one
.
wq
EOF
This requires some support from your file system, but
sed -i '10 r /dev/stdin' outputfile <<EOF
additional
lines
EOF
would work. However, if you are specifying the text directly in your script rather than a real file, the a\ command would probably be more appropriate:
sed -i '10a\
additional\
lines\
' outputfile
I need to write the output of a command to a specific line in a document. I can not just append it like so COMMAND | cat >> file, I need it to be added between two lines without replacing one or the other. I'm sure you must be able to do this via sed.
The following solution works when the output of COMMAND is only 1 line (inserting to line 4):
COMMAND | sed -i "4i \`cat` FILE"
Use that command:
command | sed -i '3r /dev/stdin' file
That inserts text after the 3rd line and reads from stdin (all output from command).
I have a file with 1 line of text, called output. I have write access to the file. I can change it from an editor with no problems.
$ cat output
1
$ ls -l o*
-rw-rw-r-- 1 jbk jbk 2 Jan 27 18:44 output
What I want to do is replace the first (and only) line in this file with a new value, either a 1 or a 0. It seems to me that sed should be perfect for this:
$ sed '1 c\ 0' output
0
$ cat output
1
But it never changes the file. I've tried it spread over 2 lines at the backslash, and with double quotes, but I cannot get it to put a 0 (or anything else) in the first line.
Sed operates on streams and prints its output to standard out.
It does not modify the input file.
It's typically used like this when you want to capture its output in a file:
#
# replace every occurrence of foo with bar in input-file
#
sed 's/foo/bar/g' input-file > output-file
The above command invokes sed on input-file and redirects the output to a new file named output-file.
Depending on your platform, you might be able to use sed's -i option to modify files in place:
sed -i.bak 's/foo/bar/g' input-file
NOTE:
Not all versions of sed support -i.
Also, different versions of sed implement -i differently.
On some platforms you MUST specify a backup extension (on others you don't have to).
Since this is an incredibly simple file, sed may actually be overkill. It sounds like you want the file to have exactly one character: a '0' or a '1'.
It may make better sense in this case to just overwrite the file rather than to edit it, e.g.:
echo "1" > output
or
echo "0" > output