Executing sed command inside a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing sed command inside a bash script
# 1  
Old 12-13-2018
Executing sed command inside a bash script

I want to run commands inside a bash script.

An example is
Quote:
sed -i "s/<p>//g" $fl


I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable


Code:
sed.sh regexp
 sed.sh "-i \"s/<p>//g\""



then call

Code:
sed "$regexp" $fl


Here is part the code I have written



Code:
    fraw="$fl"
     if [ -n "$arg_sedexp" ]; then
      regexp="$arg_sedexp"
      if [[ "$arg_sedexp" == *"-i"* ]]; then
        echo "sed "$regexp" $fl"
        sed "$regexp" $fl
      else
        fsed="${odr}/${flb}--sed.txt"
        fraw="$fsed"
        sed "$regexp" $fl > $fsed
      fi 
    fi

# 2  
Old 12-13-2018
What is your question?
# 3  
Old 12-13-2018
quotes-inside-quotes is usually a bad idea. The shell doesn't handle them - or any other shell syntax - in variables as a security measure. You can force it to do so, but that often ends up evaluating a lot of things you don't want it to.

There's probably a way to do this without resorting to quotes-in-quotes. What, exactly, are you trying to accomplish? Don't say "quotes inside quotes", don't say "passing arguments", etc, etc. What is your ultimate goal?

By my best guess, I'd do something like this:

Code:
#!/bin/bash

while [ "$#" -gt 0 ]
do
case "$1" in
-*)    # Collect all -i, -r, etc arguments
        SEDARGS="$SEDARGS $1"
        shift
        ;;
*)    break ;;
esac
done

REGEX="$1"

if [ -z "$SEDARGS" ]
then
        echo sed "$REGEX" "$FILE"
else
        echo sed $SEDARGS "$REGEX" "$FILE"
fi

Run it like:

Code:
./myscript.sh -r -i "regex"

No need for quotes-in-quotes as it takes the arguments as given.

Last edited by Corona688; 12-13-2018 at 12:44 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 12-13-2018
Have done it this way as suggested.

Code:
./myscript.sh -r -i "regex"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies

2. Shell Programming and Scripting

Run bash command inside zsh script

Hi, I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh). I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory " eve though I merged the whole... (7 Replies)
Discussion started by: kamcamonty
7 Replies

3. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

4. UNIX for Dummies Questions & Answers

Write pid and command name to a txt file while executing a bash script

Hi All, Just have a requirement, I am executing a bash shell script, my requirement is to catch the pid and job name to a txt file in the same directory, is there anyway to do it? please help me out. Regards Rahul ---------- Post updated at 08:42 AM ---------- Previous update was at... (2 Replies)
Discussion started by: rahulkalra9
2 Replies

5. Shell Programming and Scripting

Bash - command is executing but do nothing without complaining

Hello. This part of script do nothing but no complain. Here the directory content : The script run by root cd /root CMD="rmdir -p -v --ignore-fail-on-non-empty .ssh" echo $CMD $CMD The ouput linux:~/bin # ./test_05 rmdir -p -v --ignore-fail-on-non-empty .ssh rmdir: removing directory,... (3 Replies)
Discussion started by: jcdole
3 Replies

6. Shell Programming and Scripting

Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so: ./RunDiag.js param1:'string one here' param2:'string two here' I have a shell script where I invoke the above command. I can run it in a script as simple as this #!/bin/bash stuff="./RunDiag.js... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

7. Programming

Executing a awk command inside perl script --

Hello experts I want to execute a awk command, which reads from txt files and sums the numbers from the first column for those listed only inside a <init> block -- The awk command is like awk '/<\/?init>/{x = !x}x{a++}x && a > 2{sum+=$1}END{printf"%E" "\n", sum} So, I want to execute... (2 Replies)
Discussion started by: Alkass
2 Replies

8. Shell Programming and Scripting

Bash: executing a command based on organized output

Just learning scripting. I need to remove duplicate managed printers using lpadmin. I have the following script (it's rough and probably a better way to do it) that returns the values as IP (column 1) Printer Name (column 2).command: lpstat -v | grep -E ... (6 Replies)
Discussion started by: Zookpr
6 Replies

9. UNIX for Dummies Questions & Answers

Problem with executing command inside a cron job

Hi All, I have scheduled a script in cron which writes output to the below file. ....>> /data/Target/wrapper_invoke_ds_job_`date '+%Y%m%d'`.ksh_out 2>&1 But the date command is not getting resolved in the format specified. It just resolves to the following. wrapper_invoke_MQ_ds_job_Tue... (3 Replies)
Discussion started by: pkm_oec
3 Replies

10. Shell Programming and Scripting

Sed inside bash script - accessing multiple files

I have a file (email) containing email addresses. I have a second file (terms) that contains simple regular expressions and words/characters. Here are some examples: \.trainee \.group \.web I want to go through email and delete lines containing the expressions/words from terms and write... (1 Reply)
Discussion started by: manouche
1 Replies
Login or Register to Ask a Question