Bash and sed compatibility


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash and sed compatibility
# 1  
Old 07-09-2013
Bug Bash and sed compatibility

Hi i am having a stange problem.
Basically for my sed script used within bash i can unly use a single Quote with sed '. (if i use " then i get command garbled error with sed )
The problem with this is BASH does not interprit variables within single quote ' so we cannot use any of the BASH variables in sed ?

Its frustrating me, please take a look at the sample

Sample 1[code]
Code:
#!/bin/bash
while read job_nm time
do
   for file in `cat jil_file_nm_list`
   do
      match=`grep $job_nm $file`
      if [ ! -z "$match" ]; then
         echo  match found
         sed "/^insert_job:[ ,\t]$job_nm/ a\
         test line 1 \
         test line2 " wr.sfa.apr.jil
      else
           echo match not found
      fi
   done
done < mxtm.out.sample

When i run this the error is
Code:
]
bash-3.2$ bash maxtime_bash.sh
match found
sed: command garbled: /^insert_job:[ ,\t]WR#cmd#sfa_2_1_xtrapr/ a test line 1 test line2
match not found
match not found
match not found

Sample 2

Code:
#!/bin/bash
while read job_nm time
do
for file in `cat jil_file_nm_list`
do
match=`grep $job_nm $file`
if [ ! -z "$match" ]; then
echo  match found
sed '/^insert_job:[ ,\t]$job_nm/ a\
test line 1 \
test line2 ' wr.sfa.apr.jil
else
       echo match not found
fi
done
done < mxtm.out.sample


When i run sample2 $job_nm is not evaluated !

Code:
+ sed '/^insert_job:[ ,\t]$job_nm/ a\
 test line 1 \
test line2 ' wr.sfa.apr.jil
/* delete_job: WR#cmd#sfa_2_1_xtrrdw_apr    job_type: c */


How do i fix scenario 2 or for that matter 1.



Moderator's Comments:
Mod Comment Next time : Please use code tags for you data and code

Last edited by vash; 07-09-2013 at 12:59 PM.. Reason: indent code after code tagging...
# 2  
Old 07-09-2013
Sed is not 'incompatible' with single or double quotes. Quotes no longer exist by the time sed gets the string. They do alter the interpretation of the string -- as you can see when it spits it back out in the error. It's putting it all on one line.

I think you need to remove the backslashes \ separating lines. Multi-line strings are actually valid in Bourne shell, but putting the backslashes there (inside double quotes) tells it to cram it all in one single line.

Single quotes, which escape nothing ever, just put in literal backslashes and split it across lines anyway.
# 3  
Old 07-09-2013
bash and sed

Corona
The multiline is for the sed the append command requires a new line after a

This will not solve my problem of resolving the job_nm variable any way would it ?
# 4  
Old 07-09-2013
Quote:
Originally Posted by vash
Corona
The multiline is for the sed the append command requires a new line
So remove the backslash, because, as stated above, they are preventing it from actually being multiple lines.

Code:
$ echo "a\
b\
c"

abc

$ echo "a
b
c"

a
b
c

$

Quote:
This will not solve my problem of resolving the job_nm variable any way would it ?
The problem is, simply, that variables do not expand inside single quotes.

Remove the \ from the double quotes and see if it works.
# 5  
Old 07-09-2013
sed bash

No removing \ from " dint work as sed complaining about command
here is a smaller version i am testing with

Code:
#!/bin/bash
var=WR#cmd#sfa_2_1_xtrapr
sed "/^insert_job:[ ,\t]$var/ a
> test line 1 " wr.sfa.apr.jil

bash-3.2$ bash expsn
sed: command garbled: /^insert_job:[ ,\t]$var/ a

---------- Post updated at 12:08 PM ---------- Previous update was at 12:07 PM ----------

Quote:
Originally Posted by Corona688
So remove the backslash, because, as stated above, they are preventing it from actually being multiple lines.

Corona - the \ is Used withn SED not BASH
i understand what you are tryign to say please see my code properly ..and i mentioned its th eSED that requires it not BASH ..

Code:
$ echo "a\
b\
c"
 
abc
 
$ echo "a
b
c"
 
a
b
c
 
$

The problem is, simply, that variables do not expand inside single quotes.

Remove the \ from the double quotes and see if it works.
# 6  
Old 07-09-2013
Does your sed command need the backslashes? You may need to double them up, then.

Code:
$ echo "a\\
b\\
c"

a\
b\
c

$

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 07-09-2013
Quote:
Originally Posted by Corona688
Does your sed command need the backslashes? You may need to double them up, then.

Code:
$ echo "a\\
b\\
c"
 
a\
b\
c
 
$

Thanks Thats it .. dint hit me untill i saw the solution .. dang ! thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use foreach with sed in a bash script

I want to extract data from a ASCII file that looks like the one provided here (see input.txt). For this purpose I used sed commands. I want to chain the sed commands into a script that I can call with custom variables, instead of having to run it multiple times (Need to run the code for 30*24 =... (1 Reply)
Discussion started by: learningtocode
1 Replies

2. Shell Programming and Scripting

Backward compatibility issue with GNU bash, version 4.3.42(5)

Hi, I have made a snake game on below bash version. But it is not backward compatible. Can someone suggest a version which is most commonly used and is backward compatible so that i can change my code and share again? I have attached the game script. If someone can run it please also suggest... (5 Replies)
Discussion started by: amit14august
5 Replies

3. Debian

Using sed with bash variables

Hi Guys I have another problem I'm trying to solve and hope that some one can help me here. This is the scenario: I have a file and I want to add a line on the 3rd line of the file using a bash script. but instead its adding the the bash variable $WEBSITE. Below is the bash script I'm... (6 Replies)
Discussion started by: linuxjunkie
6 Replies

4. Shell Programming and Scripting

sed not working in a bash script

Hi friends, I have two files - input and commands I want to read the input and replace a value in it with the contents in commands. My script is like this. Instead of printing the value in the commands file, it is simply printing $cmd in the output file. Any pointers are highly... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. Shell Programming and Scripting

Delete lines with SED in Bash?

Hello everyone I'm doing a program in bash and wanted to know how can I do to delete document lines the words not ending in S with SED, ie, show only those ending with the letter S. I probe with: sed -e /$.*/d "$file" | more to delete all lines NOT ending in S but not work!... (3 Replies)
Discussion started by: adiegorpc
3 Replies

6. Shell Programming and Scripting

Bash sed problem

Hi, I need to escape slashes in my text, so I use this line: search_string=`echo $var | sed 's@/@\\\/@g'`I expect that to replace a slash with a backslash followed by a slash. That works nicely, but it has a problematic side-effect. If there are two spaces in the var it replaces them with... (3 Replies)
Discussion started by: RickS
3 Replies

7. Shell Programming and Scripting

using Sed in Solaris bash

I am trying to execute a script with sed that works well in ksh(Linux) however in bash(solaris 8) though it does not give any errors the output file becomes 0 byte. header of the script: ksh:2$ head news.ksh #!/bin/ksh... (2 Replies)
Discussion started by: acharania2011
2 Replies

8. UNIX for Dummies Questions & Answers

$ in sed under tcsh vs bash

In bash, I can match the ' character in a substition involving the line ending symbol $, easily. In tcsh I ran into a problem. Code: sed "s/$/'/g" filename sed "s/$/'/g" < filename sed -e "s/$/'/g" filename Unmatched '. Where can I find out why this is the case? (2 Replies)
Discussion started by: uiop44
2 Replies

9. Shell Programming and Scripting

BASH -- sed -- variable

Hi, I'm new at bash scripting -- can anyone here help me about the sed command? I need to be able to edit and or delete a text from an outside file ie file.txt -- I'm passing a variable and not a string I was thinking of something like echo -n "What do you want to edit?: " read edit sed... (1 Reply)
Discussion started by: Imajean
1 Replies

10. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies
Login or Register to Ask a Question