On Linux, use the ** sed command to insert characters into various parts of a line **, which is a method using a for statement. I think it can be applied to other commands. (There seems to be a simpler way ...)
insert.sh (You can also copy and paste on the command line instead of a shell script)
#!/bin/bash
cat sequence.txt > sequence2.txt
for int in 15 12 9 6 3
do
echo "$(sed -E "s/(^.{$int})/\1-/g" sequence2.txt)" > sequence2.txt
done
##Every 3 characters"-"Insert
## sequence.txt -> ("-"Insert) -> sequence2.txt
Execution result
$ cat sequence.txt
xxxx5xxx10xxx15
xx3xx6xx9x12x15
xxxxAxxxxAxxxxA
xxAxxAxxAxxAxxA
$ ./insert.sh ; cat sequence2.txt
xxx-x5x-xx1-0xx-x15-
xx3-xx6-xx9-x12-x15-
xxx-xAx-xxx-Axx-xxA-
xxA-xxA-xxA-xxA-xxA-
sed -E" s / (^. {$ Int}) / \ 1- / g "sequence2.txt> sequence2.txt
Normally, the contents of ** sequence2.txt ** will be empty, but by setting <br>, ** sequence2 You can successfully overwrite the .txt ** without being empty and output. <br> <br> <font color = gray> (Empty data is output in the first place when
> sequence2.txt is executed, and the operation ** empty the contents of ** sequence2.txt before
sed** Because it is done (as a result, it is
sed to empty data). ʻEcho" $ (command) "
The contents of are executed before
> sequence2.txt`, so this will successfully overwrite ** sequence2.txt **) </ font> $ int
** of the for statement in the sed
command, you need to use " "
(using ''
is a variable in the variable. Because it is recognized as just a character string)There seem to be various other methods for outputting to the file with the same name, so if you are interested, please refer to the link below. (1st link)
Regarding> 1.
Process the file with bash and overwrite the result with the same file
Redirect to file with the same name
Regarding 2.
[[bash] Difference between "" "," '", and" `--Rather rice school](https://blog.goo.ne.jp/01_mai/e/ a4f9f01fb647066d0c7eb37e8ae0a254)
Recommended Posts