Sponsored Content
Full Discussion: Bash and sed compatibility
Top Forums Shell Programming and Scripting Bash and sed compatibility Post 302830731 by vash on Tuesday 9th of July 2013 11:41:29 AM
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...
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 04:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy