Please help me with the below problem
The required output is :
But I am getting the below error:
Please help.
I don't understand what you're trying to do. Why would a script that attempts to use a sed substitute command to replace the 1st occurrence of the character a with the contents of an undefined variable be expected to change the digit 1 in your sample input to a <space> character???
If you want to change the 1st occurrence of the digit 1 on each input line to the value you have assigned to the shell variable a (assuming that $a does not expand to a string containing any /, \, &, or NUL characters; you could use:
Note that unless you quote the entire substitute command, you can't include blanks in the substitution string because the shell will see sequences of blanks in the substitute command as field separators; not as data included in the replacement string.
Last edited by Don Cragun; 11-07-2016 at 08:24 PM..
Reason: add missing space between sed and its operand
I don't understand what you're trying to do. Why would a script that attempts to use a sed substitute command to replace the 1st occurrence of the character a with the contents of an undefined variable be expected to change the digit 1 in your sample input to a <space> character???
If you want to change the 1st occurrence of the digit 1 on each input line to the value you have assigned to the shell variable a (assuming that $a does not expand to a string containing any /, \, &, or NUL characters; you could use:
Note that unless you quote the entire substitute command, you can't include blanks in the substitution string because the shell will see sequences of blanks in the substitute command as field separators; not as data included in the replacement string.
I have this requirement to put space as a substitution for some cases.
Isn't there any option to give space to a variable and use it in sed?
Because, I am using sed for replacing other strings in my script, including space is one of the requirement.
In the above code, I am trying to replace '1' with string a (some string / space ) depending on the input read.
I have this requirement to put space as a substitution for some cases.
Isn't there any option to give space to a variable and use it in sed?
Because, I am using sed for replacing other strings in my script, including space is one of the requirement.
In the above code, I am trying to replace '1' with string a (some string / space ) depending on the input read.
Yes, there is a way to do it. Do it exactly as I showed you in post #2 in this thread.
This User Gave Thanks to Don Cragun For This Post:
Note that unless you quote the entire substitute command, you can't include blanks in the substitution string because the shell will see sequences of blanks in the substitute command as field separators; not as data included in the replacement string.
I'd like to respectfully disagree: you can do that, but it takes a little effort with the quoting to make sure the string doesn't undergo the shells field splitting:
Of course all the usual problems with shell strings used as sed program(part)s persist: metacharacters, which might lead to the regexp meaning something very different from the expected, etc..
I'd like to respectfully disagree: you can do that, but it takes a little effort with the quoting to make sure the string doesn't undergo the shells field splitting:
Of course all the usual problems with shell strings used as sed program(part)s persist: metacharacters, which might lead to the regexp meaning something very different from the expected, etc..
I hope this helps.
bakunin
Hi bakunin,
You are absolutely correct in saying that the entire substitute command doesn't have to be quoted (only characters subject to field splitting, pathname expansions, and other expansions that have already been discussed have to be quoted), but note that in your example:
you have the entire substitute command quoted (some in single quotes and some in double quotes, and backslashes can also be used to quote characters in strings). But, we could have written this specific command as:
(without the single quotes) and still get the same results. But, if the substitute pattern had contained bracket expressions, asterisks, question marks, or parentheses, they would have to be quoted to protect against possible pathname expansions. I don't know of any case where someone writing a script using sedneeds to write an unquoted substitute command to make it work correctly. Therefore, I generally tell noobies to always quote the entire substitute command. They just have to decide which quotes are appropriate to use to get dollar signs in the pattern and in the replacement text to work as desired.
Therefore, I generally tell noobies to always quote the entire substitute command.
D'accord!
I understood your first quote to mean you have to enclose everything into one quotation. Personally. i prefer to enclose to biggest-possible part of my sed-scripts always into single-quotes (to not quote them at all didn't even come to my mind, even if this might be possible for a few select scripts, as you pointed out) and only the parts where i need parameter expansion i enclose in double-quotes. This results in the way i wrote it, with the alternating single- and double-quotes.
Anyway, i take it, we used different words to mean the same.
Hie everyone
i have a complex sed command line which work fine for a static value given
example
sed -n '/SQL ID\:\ 1111111111111/!b;:a;/\*\*\*\*\*\*\*\*\*\*\*\*/!{$!{N;ba}};{/*/p}' ./MyInputFile.txt
what i want is something like that
sed -n '/SQL ID\:\... (2 Replies)
Hi All,
How can i use a variable in a sed command ? I cant seem to get it to work as at present its just printing $i at the start of every line rather than the variable $1.
sed -e "s/^/\$i,|/"
Any help would be appreciated. (3 Replies)
Hi All,
Can I use a variable (using this variable as a counter) in sed cmd?
something like below
sed -n '${COUNT}p'
But it's not working. Pls help.
Thanks.
Regards,
Amee
export COUNT=1
export COUNTER=`cat ${BINPATH}/$ALLOC_FILE.clean_1 | wc -l | tr -d " "`
while ... (3 Replies)
Hi,
I want to insert some text in the begning of each line. But issue is the text that i want to insert is stored into one variable. so my command look like
constr="`date | awk '{print $3"-"$2"-"$6}'`",MXGBTST1" "
sed 's/^/\$constr/g' alert_temp.csv
but sed command instead of taking... (3 Replies)
I have file File1.txt in which i have to replace a text using sed command
File1.txt contents
EURAMOUNTTOBEREPLACED
I have a AIX shell script for replacing the text AMOUNTTOBEREPLACED
Contents of the shell script
sum=27
sed 's/AMOUNTTOBEREPLACED/"$sum"/g' File1.txt >> temp
mv temp... (5 Replies)
I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename?
#!/bin/bash
filename=`echo $0`
/usr/bin/sed '/#include/ {
i\
the filename is `$filename`
}' $1
exit 0 (8 Replies)
Hi guys,
I wanted to pass a variable to the sed command which tells which line to be deleted.
a=2;
echo $a;
sed '$ad' c.out
it is throwing an error.
sed: 0602-403 "$a"d is not a recognized function.
I even tried "$a" and \$a.. but it is of no use.
Can you please correct me... (6 Replies)
I want to instert Category:XXXXX into the 2. line
something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong:
sed "2i\\${n}Category:$cat\n"
Sample:
Titel Blahh Blahh abllk sdhsd sjdhf
Blahh Blah Blahh
Blahh
Should look like... (2 Replies)
Hi ,
Seen some of wonderful posts. Please have look at my problem.
I have a text file ketkee.txt , which look like below.
GATES := apple mango banana
XMLCHECK := red blue green pink
My requiement is to find the line that has XMLCHECK in it. and append two variables followed by a space... (2 Replies)
how much do you know on using the sed commands within a diff command? I'm currently using one long command when robot runs that... when the diff command is ran that the < and > signs at the beginning of each line are changed to the environment name that they came from... making it easier to... (2 Replies)