I am trying to add
-
paths:
- /var/log/consumer.log
document_type: consumer
input_type: log
after prospectors: in my file. I am using command:
sed -i '/prospectors:/a\ \ \ \ \-
\ \ \ \ \ \ paths:\
\ \ \ \ \ \ \- \/var\/log\/consumer.log
\ \ \ \ \ \ document_type: consumer
\ \ \ \ \ \ input_type: log' new.txt
But the above command gives following error:
sed: -e expression #1, char 62: unknown command: `\'
How can I achieve the desired?
In classic (POSIX) sed, each line of data appended needs to be on its own line after the a command, and all lines except the last need a backslash at the end to indicate that the data continues. GNU sed allows some information on the same line as the a command, but otherwise follows the rules.
There's an additional wrinkle: sed removes leading blanks from the data. To get the leading blanks, you can use backslash-blank at the start.
Hence, you can end up with:
sed -i '/prospectors:/a \
\ -\
\ paths:\
\ - /var/log/consumer.log\
\ document_type: consumer\
\ input_type: log' new.txt
The leading blanks are ignored; the backslash is deleted; the following blanks are copied to the output. Thus given an input containing just a line containing prospectors:, the output is:
prospectors:
-
paths:
- /var/log/consumer.log
document_type: consumer
input_type: log
Obviously, you can adjust the spacing to suit yourself.
I note that BSD sed requires a suffix after the -i option; it can be -i '' to get an 'empty string' suffix. To be portable between GNU and BSD sed, use -i.bak (no space; GNU sed doesn't like the space; BSD sed accepts the attached suffix, but you can't attach an empty suffix). And the -i option is not mandated by POSIX, so it isn't available on all Unix-like systems. If you're only using GNU sed, you don't have to worry about this trivia.
Related
I'm trying to compile the U-boot 2020.07 with option CONFIG_USE_DEFAULT_ENV_FILE=y and with the path to file which contains new U-boot environments records.
u-boot-suniv-spiflash/1_v2020.07-r0/git/scripts/Makefile.build
obj=scripts/basic | /bin/sh: 1: xxd: not found
I try to compile manually the same U-boot with the same Yocto toolchain and the compilation success U-boot works with replaced env records.
The problem is related to Makefile. I found somewhere some solution which allows me to compile success but with empty environment records in U-boot after boot-up.
The problematic syntax is
define filechk_defaultenv.h
(grep -v '^#' | \
grep -v '^$$' | \
tr '\n' '\0' | \
sed -e 's/\\\x0\s*//g' | \
xxd -i ; echo ", 0x00" ; )
ended
the solution from internet is
define filechk_defaultenv.h
(grep -v '^#' | \
grep -v '^$$' | \
tr '\n' '\0' | \
sed -e 's/\\\x0\s*//g' | \
xxd -i | \
sed -r 's/([0-9a-f])$$/\1, /'; \
echo "0x00" ; )
ended
Unfortunately, these environment records are empty after U-Boot's start-up.
Do You have knowledge of what am I doing wrong?
sed -i 's|from_infura_hex=?|from_infura_hex=$(curl -s -X POST --connect-timeout 5 -H "Content-Type: application/json" --data \'{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}\' https://ropsten.infura.io/X/X | jq .result | xargs)|' /home/ec2-user/LastBlockNode.sh
I tried to execute this command but I always get this error:
-bash: syntax error near unexpected token `)'
The purpose of this command is to modify the value from_infura_hex=? in the script LastBlockNode.sh by the curl command.
Can anyone help with this sed command?
If you choose a pipe character | as a delimiter for s command,
the character should not appear in pattern or replacement without escaping. As you are using | as a pipeline in your command, it is better to pick other character such as #.
You cannot nest single quotes even if you escape it with a backslash.
In order to use a command substitution within the replacement,
you need to say sed -i '/pattern/'"$(command)"'/', not
sed -i '/pattern/$(command)/'.
Then would you please try something like:
sed -i 's#from_infura_hex=?#from_infura_hex='"$(curl -s -X POST --connect-timeout 5 -H "Content-Type: application/json" --data "{\"jsonrpc\":\"2.0\",\"method\":
\"eth_blockNumber\",\"params\":[],\"id\":1}" https://ropsten.infura.io/X/X | jq .result | xargs)"'#' /home/ec2-user/LastBlockNode.sh
But it will be safer and more readable to split the command into
multiple lines:
replacement="$(curl -s -X POST --connect-timeout 5 -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' https://ropsten.infura.io/X/X | jq .result | xargs)"
sed -i 's#from_infura_hex=?#from_infura_hex='"$replacement"'#' /home/ec2-user/LastBlockNode.sh
Please note I have not tested the commands above with the actual data.
If either of them still do not work, please let me know with the error message.
I'm hoping there's someone that could help with this. I'm most certain the question have been asked however, i'm having some difficulty understanding some of the answers.
I have the following text in file.txt
value1=192.168.1.2
value2=10.1.1.15
I'd like to replace those ip address and add value3=10.224.100.5 if value3 doesn't exist, using sed.
What i have so far or at least tried.
sed \
-e '/^#\?\(\s*value1\s*=\s*\).*/{s//\newvalue/;:a;n;ba;q}' \
-e '$avalue1=newvalue' \
-e '/^#\?\(\s*value2\s*=\s*\).*/{s//\newvalue/;:a;n;ba;q}' \
-e '$avalue2=newvalue' \
-e '/^#\?\(\s*value3\s*=\s*\).*/{s//\newvalue/;:a;n;ba;q}' \
-e '$avalue3=newvalue' file.txt
This works fine if value(1,2,3) doesn't exist however, if value1 exists in file.txt, it stops at 1.
I'm assuming its because of the ;q
Any advice please? i'm really having a hard time getting this.
One way with awk:
awk -v v1="new1" -v v2="new2"
'BEGIN{FS=OFS="=";addV3=1}
$1=="value1"{$2=v1}
$1=="value2"{$2=v2}
$1=="value3"{addV3=0}7;
END{if(addV3)print "value3=newV3"}' file
test with your example:
kent$ cat f
value1=192.168.1.2
value2=10.1.1.15
kent$ awk -v v1="new1" -v v2="new2" 'BEGIN{FS=OFS="=";addV3=1}$1=="value1"{$2=v1}$1=="value2"{$2=v2}$1=="value3"{addV3=0}7;END{if(addV3)print "value3=newV3"}' f
value1=new1
value2=new2
value3=newV3
...
tomcat.javaoptions=-Djava.net.preferIPv4Stack\=true \
-Djava.net.preferIPv6Addresses\=false \
-Dcom.sun.management.jmxremote.port\=12345 \
-Djava.rmi.server.hostname=${application.hostname}
...
I need add new line to the end of tomcat.javaoptions with sed. I have to use regex, because I do not know how java options will look originally. i know only that it starts from tomcat.javaoptions= and can have multiple lines. Any idea?
EDITED:
I need to add new line
...
tomcat.javaoptions=-Djava.net.preferIPv4Stack\=true \
-Djava.net.preferIPv6Addresses\=false \
-Dcom.sun.management.jmxremote.port\=12345 \
-Djava.rmi.server.hostname=${application.hostname} \
-agentpath:/opt/agent/agent.so,name=agent
...
I tried it just to add only "-agentpath" but no luck
sed -i "/^tomcat.javaoptions=(.*/n*)*/s/$/ \\\\\n -agentpath/g" file
I don't know what the end of tomcat.javaoptions condition is, but I modify your script, so it works:
sed -r -i -e "/^tomcat.javaoptions=(.*\n*)*/s/$/ \n -agentpath/g" File
Changes:
Remember to add -r parameter to sed,
replace your /n with \n.
Is there a way to get relative paths to resulted coverage.xml (or strip off prefix) using coverage.py?
I could only manage to sed the console output. The bash code below will expand "../myrootmodule" and remove it from the coverage output. It will also adjust the spaces to preserve the columns alignment. You must adjust the myrootmodule path (first line only).
prefix="$$(readlink -e $$(dirname $$(pwd)))/myrootmodule/"; \
coverage report \
| sed "s|$$prefix||" \
| sed "s/^-\{$${#prefix}\}//" \
| sed "s/^Name \{$${#prefix}\}/Name/" \
| sed "s/^TOTAL \{$${#prefix}\}/TOTAL/"
As it is big, I include it in tests/Makefile.