spacial character replacement using sed or awk - awk

I have a string in a dat file
Times-New $\Gamma$
and I want to replace it using sed or awk (I would prefer to use awk) utility with
/Symbol G
I could replace only one $ sign but failed for the remaining text.
So what I want is
sed 's/Times-New $\Gamma$/Symbol G/g' case.dat
Could you please help me?

Could you please try following, written and tested with shown samples. We need to escape \ and $ to make awk treat them as a literal character in program.
awk '{gsub(/Times-New \$\\Gamma\$/,"/Symbol G")} 1' Input_file
OR as per OP's comment try(Thanks to anubhava sir for adding solution):
awk '{gsub(/Times-New[ \t]+(G|\$\\Gamma\$)/, "/Symbol G")} 1' Input_file
With GNU sed try:
sed -E 's/Times-New \$\\Gamma\$/Symbol G/g' Input_file

Related

How to print specific string from a sentence using awk

I have the following sentence within a file
FQDN=joe.blogs.com.
How can I print the string "joe"
I have tried using -->> awk -F"=" '{print $2}' file
but this returns joe.blogs.com as "=" is the delimiter.
Is it possible to use 2 delimiters on the same line?
You might use regular expression as FS. Let file.txt content be
FQDN=joe.blogs.com.
then
awk 'BEGIN{FS="[=.]"}{print $2}' file.txt
output
joe
In case you are ok with sed, could you please try following.
sed 's/.*=\([^.]*\)\..*/\1/' Input_file
With GNU grep and using its -oP flag we could try following too.
grep -oP '(.*=)\K([^.]*)' Input_file
You could use GNU grep:
grep -oP '(?<=FQDN=)[^.]+' file
^ all characters up to a '.'
^ lookbehind for 'FQDN='
^ only print match and Perl style regex
Or with Perl:
perl -lne 'print $1 if /(?<=FQDN=)([^.]+)/' file
With awk I would probably do:
awk 'BEGIN{FS="[.=]"} /FQDN=/{print $2}' file
why not keeping it simple and pipe awk?
awk -F"=" '{print $2}' | awk -F"." '{print $1}'
can I use two field delimiters on one line?
No. You may do further string manipulation as post processing, or you could use a regex as field delimiter.
Another option is to use awk's split function:
awk -F= '{ split($2,map,".");print map[1] }' file
Split the second = separated field into the array map using "." as the delimiter. Print the first index of the array.

How do I add characters between strings in bash?

Tell me how to add it using the sed command.
[Before revision]
BONDING_OPTS="mode=1 miimon=200 primary=ens192"
[After revision]
BONDING_OPTS="mode=1 miimon=200 primary=ens192 primary=eth0"
I have succeeded so far.
sed "/BOND/s/$/primary=eth0/g" /etc/sysconfig/network-scripts/ifcfg-bond0
Output:
BONDING_OPTS="mode=1 miimon=200 primary=ens192"primary=eth0
Thanks for adding your effort in your post. Could you please try following.
awk '/BOND/{sub(/\"$/," primary=eth0&")} 1' Input_file
In case you want to save output into Input_file itself then try:
awk '/BOND/{sub(/\"$/," primary=eth0&")} 1' Input_file > temp && mv temp Input_file
With sed:
sed '/BOND/s/\"$/ primary=eth0&/' Input_file
Please use either sed -i (to save output into Input_file itself) OR use sed -i.bak (to save output into Input_file itself taking a backup of existing Input_file).
sed '/BOND/s/"$/ primary=eth0"/'
use single quotes, string inside double quotes is subject to shell interpretation before it is passed to sed and that leads to mistakes often
See also: Difference between single and double quotes in Bash
since you need to insert before something at end of line, you need to add that condition before applying $ anchor - which is " in this case
In case you have a non-trivial string or regex before the anchor, you can use & to get back that matched string in replacement section instead of duplicating the text
sed '/BOND/s/"$/ primary=eth0&/' ip.txt
For in-place editing, see sed in-place flag that works both on Mac (BSD) and Linux

How to edit special character in Linux files using with SED?

File name: File.txt
Content:
Vignesh Mani Bani Ravi
Result should come with,
"Vignesh", "Mani", "Bani", "Ravi"
Can anyone help me how can I get the expected result?
I have tried this command.
sed -e 's/^ /" ",/g' File.txt
It's not working. Please advise me.
$ sed 's/ /", "/g; s/^\|$/"/g' file
"Vignesh", "Mani", "Bani", "Ravi"
1st solution: If your actual Input_file is same as shown sample then following may help you here.
awk -v s1='"' -v s2=',' '{gsub(/ /,s1 s2 OFS s1);gsub(/^|$/,s1)} 1' Input_file
2nd solution:
awk -v s1='"' 'BEGIN{OFS=s1", "s1} {gsub(/^|$/,s1);$1=$1} 1' Input_file
I would do it with awk:
awk -v OFS=", " -v q='"' '{for(i=1;i<=NF;i++)$i=q $i q}7' file
This might work for you (GNU sed):
sed 's/\S\+/"&"/g;s/\s\+/, /g' file
Surround non-white space by double quotes. Replace each set of white space by a comma followed by a space.

how to extract line portion on the basis of start substring and end substring using sed or awk

I have a multiline file with text having no spaces.
Thereisacat;whichisverycute.Thereisadog;whichisverycute.
Thereisacat;whichisverycute.Thereisadog;whichisverycute.
I want to extract string between cat and cute (first occurrence not second) that is the output is
;whichisvery
;whichisvery
I am close to getting it but I end up getting string from cat to the last cute with the command from here.
sed -e 's/.*cat\(.*\)cute.*/\1/'
I am getting
;whichisverycute.Thereisadog;whichisvery
;whichisverycute.Thereisadog;whichisvery
How can I get the text from cat to the first occurrence of cute not last?
Given the input you posted all you need is:
$ awk -F'cat|cute' '{print $2}' file
;whichisvery
;whichisvery
In sed:
sed 's/cute.*//;s/.*cat//' Input_file
In awk:
awk '{sub(/cute.*/,"");sub(/^.*cat/,"");print}' Input_file

awk sed grep to extract patten with special characters

I am trying to understant the switchs and args in awk and sed
For instance, to get the number next to nonce in the line form the file response.xml:
WWW-Authenticate: ServiceAuth realm="WinREST", nonce="1828HvF7EfPnRtzSs/h10Q=="
I use by suggestion of another member
nonce=$(sed -nE 's/.*nonce="([^"]+)"/\1/p' response.xml)
to get the numbers next to the word idOperation in the line below I was trying :
idOper=$(sed -nE 's/.*idOperation="([^"]+)"/\1/p' zarph_response.xml)
line to extract the number:
{"reqStatus":{"code":0,"message":"Success","success":true},"idOperation":"185-16-6"}
how do I get the 185-16-6 ?
and if the data to extract has no ""
like the 1 next to operStatus ?
{"reqStatus":{"code":0,"message":"Success","success":true},"operStatus":1,"amountReceived":0,"amountDismissed":0,"currency":"EUR"}
Following awk may help you on same.
awk -F"\"" '/idOperation/{print $(NF-1)}' Input_file
Solution 2nd: In sed following may help you on same.
sed '/idOperation/s/\(.*:\)"\([^"]*\)\(.*\)/\2/' Input_file
EDIT: In case you want to get the digit after string operStatus then following may help you on same.
awk 'match($0,/operStatus[^,]*/){;print substr($0,RSTART+12,RLENGTH-12)}' Input_file
Using grep perl-style regexes
grep -oP "nonce=\"\K(.*)?(?=\")" Input_file
grep -oP "idOperation\":\"\K(.*)?(?=\")" Input_file
If the input is json you can use jq
jq .idOperation Input_file