How can I combine sed commands to a shell script
Solution 1
The variable thing is easy:
F=delhi222517.txt
sed -i '/^Total/d' "$F"
...
Or if you want to pass the name of the file as argument to your script:
F="$1"
sed -i '/^Total/d' "$F"
...
But it is better to use the sed
options to call it only once. You can use:
sed -i \
-e '/^Total/d' \
-e '/^CBSE/d' \
-e '/^Keyword wise/d' \
... \
delhi222517.txt
Or you can write a file with the full script:
sed -i -f script.sed delhi222517.txt
Or if you feel geek enough, you can use the standard input:
sed -i -f - delhi222517.txt << EOF
/^Total/d
/^CBSE/d
/^Keyword wise/d
...
EOF
Solution 2
On the Command Line
On the command line, you can separate sed commands with semi-colons or with multiple expression arguments. As generic examples:
# Using Semi-Colons
sed -i 's/foo/bar/; s/baz/quux/' infile
# Using Multiple Expressions
sed -i -e 's/foo/bar/' -e 's/baz/quux/' infile
Write a Full-Fledged Sed Script
In general, though, if your commands are numerous, stop using one-liners and build a full-fledged sed script. For example, you could create a file named /tmp/foo.sed
containing the following commands from your question:
/^Total/d
/^CBSE/d
/^Keyword wise/d
/^wise/d
/^Select A/d
/^Enter A/d
/^(Keyword/d
/^State Name/d
/^SNo/d
/^Disclaimer/d
/^provided/d
/^at$/d
/^Designed/d
/^National/d
/^$/d
/^\t$/d
/^\s$/d
/^ /d
/^ /d
s/^\([0-9]\)/--\1/g
Then invoke your commands all at once. For example, using GNU sed:
infile='delhi222517.txt'
script='/tmp/foo.sed'
sed --in-place --file="$script" "$infile"
Solution 3
Well you can put them in a a shell script like this:
#!/bin/bash
# some sanity checks
file="$1"
sed -i '/^Total/d' "$file"
sed -i '/^CBSE/d' "$file"
sed -i '/^Keyword wise/d' "$file"
sed -i '/^wise/d' "$file"
#.. more sed commands
btw your various sed commands can be combined into 1 or fewer sed command using reges like:
sed -r -i '/^(Total|CBSE)/d' "$file"
Solution 4
Using awk
you can do all in go:
file=delhi222517.txt
awk '!/^(Total|CBSE|Keyword wise|wise)/' "$file"
Solution 5
If your script is not going to run any program other than sed your file, then this may be the cleanest way to set it up:
#!/bin/sed -f # <- run the file passed to the program from the command-line
/^Total/Id # /I is the case-insensitive flag, replacing sed -i
/^CBSE/Id
/^Keyword wise/Id
/^wise/Id
/^Select A/Id
/^Enter A/Id
...
Make the above script executable, and then just pass it the filename that you wish to convert: ./mysedscript delhi222517.txt
Ranjith Siji
A PHP Programmer using Ubuntu and Fedora . Now studying python and laravel. Trying Angular Js.
Updated on June 14, 2022Comments
-
Ranjith Siji almost 2 years
I Have to Operate the following sed commands over a file to remove some lines in that file. How can I make this a shell scipt with the file name as a variable. Or is there any simple way to do this as a shell script
sed -i '/^Total/d' delhi222517.txt sed -i '/^CBSE/d' delhi222517.txt sed -i '/^Keyword wise/d' delhi222517.txt sed -i '/^wise/d' delhi222517.txt sed -i '/^Select A/d' delhi222517.txt sed -i '/^Enter A/d' delhi222517.txt sed -i '/^(Keyword/d' delhi222517.txt sed -i '/^State Name/d' delhi222517.txt sed -i '/^SNo/d' delhi222517.txt sed -i '/^Disclaimer/d' delhi222517.txt sed -i '/^provided/d' delhi222517.txt sed -i '/^at$/d' delhi222517.txt sed -i '/^Designed/d' delhi222517.txt sed -i '/^National/d' delhi222517.txt sed -i '/^$/d' delhi222517.txt sed -i '/^\t$/d' delhi222517.txt sed -i '/^\s$/d' delhi222517.txt sed -i '/^ /d' delhi222517.txt sed -i '/^ /d' delhi222517.txt sed -i 's/^\([0-9]\)/--\1/g' delhi222517.txt