Essentially, I'm given a text file and am supposed to screw with it via sed commands. I have about 7 sed commands total and need to put them all in one shell script to produce ONE output when this script is run.

Text file to be screwed with: students.txt


dog dog dog Jamesb

So the first sed gets rid of first and last line. second sed replaces dog with DOGGGG third sed finds and prints all lines w/JamesBrady fourth sed finds and prints all lines with derghnregnrig

Sed Commands (BS Examples):

sed -e '1d' -e '$d' -e '/^$/d' students.txt
sed -n s/dog/DOGGGG/g students.txt
sed -n /JamesBrady/p students.txt
sed -n /derghnregnrig/p students.txt

The above sed commands are all to go in one shell script file, and when run are supposed to output its (cumulative?) effects on one text file (students.txt)

So far, I've tried to look up the correct procedure for doing this and have met nothing but confusion. Can someone explain in clarity and with a beginner's patience how multiple sed commands, and any commmands (awk, grep, etc.) for that matter, are supposed to fit inside a single shell script file?

Using Ubuntu 16.04, Vim. Bash shell

  • 2
    Instead show us the contents of students.txt, and your desired output from that.
    – heemayl
    Oct 11, 2016 at 6:45
  • @heemayl It has been edited.
    – Anonymous
    Oct 11, 2016 at 6:51
  • The main question is not really what the text file contains, but how to combine sed commands together in one script file so their effects accumulate into one output???
    – Anonymous
    Oct 11, 2016 at 6:57

3 Answers 3


I assume you actually want to see the line with DOGGGG since you are editing it (otherwise, why bother?)

Use { } to group commands in a block...

sed -n '{
}' students.txt



You don't need all that for your example...

sed -n '{
}' students.txt

but I guess your real file really has empty lines and stuff in the middle you don't want to print.


First of all, check those sed commands, they may not be doing what you want. For example, the second one, instead of

sed -n s/dog/DOGGGG/g students.txt

I think it should be

sed -i 's/dog/DOGGGG/g' students.txt

Read man sed and make sure that they individually work before you create your script.

Make a new file (i.e. nano yourscript.sh) and write #!/bin/bash followed by your sed commands, replacing students.txt with $1. This $1 represents the first argument when you call the script:

sed -e '1d' -e '$d' -e '/^$/d' $1
sed -n s/dog/DOGGGG/g $1
sed -n /JamesBrady/p $1
sed -n /derghnregnrig/p $1

With the correct sed commands. Now give the script execution permissions:

sudo chmod 755 yourscript.sh

And execute it, with the file name as the first argument:

sudo ./yourscript.sh students.txt

You only need sudo in this last command if your user doesn't have permission to change the students.txt.


Option 1: put each sed command in your shell script on a separate line and have them operate in place using the -i switch.

Option 2: use a single sed command line in your shell script but pass each expression using a separate -e switch.

Option 2 is preferred if your expressions are orthogonal (that is, they do not operate on the same matches).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .