Sed unknown command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed unknown command
# 1  
Old 09-17-2008
Sed unknown command

Hi everyone, I am trying to replace a string that contains a space using sed. A typical input file is:

Code:
CATEGORY=1 1:1.0
CATEGORY=2 13:1.0
CATEGORY=3 1:99.0
CATEGORY=8 14:1.0
CATEGORY=11 1:1.0

I want to use a string (as it can change) to make replacements using

Code:
toReplace='s/CATEGORY=11 /+1 /g'
sed -e $toReplace $outputFile > $outputFile.tmp

I get a command unknown error from sed. Can anyone help please?

Last edited by bakunin; 09-17-2008 at 07:16 AM.. Reason: added code tags, please use them when posting files/scripts.
# 2  
Old 09-17-2008
You need to properly quote anything with spaces or other shell specials in it. As a matter of fact, unless you really know what you are doing, get into the habit of double-quoting all variables.

Code:
sed -e "$toReplace" "$outputFile" > "$outputFile".tmp

# 3  
Old 09-17-2008
Probably the shell fails to expand your variables the way you want them to be expanded. If you have variables containing spaces use double quotes to make sure they get expanded into one argument and not several.

To understand try the following: save the following to a file countargs.sh and make it executable:

Code:
#! /bin/ksh

print - "Number of arguments passed: $#"

while [ -n $# ] ; do
     print - "Argument passed: \"$1\""
     shift
done

exit 0

Now lets experiment: what do you think is the result if you substitute the "sed" in your command with the "countargs.sh" script? What is it really?

sed has a command syntax of "sed COMMAND FILE" if you present it something like "sed s/x /y /g some_file" it will think "s/x" is the command (which leads to a syntactical error of course) "/y" is the file and the rest is ignored. Only if you write sed "s/x /y /g" "some_file" it will treat "s/x /y /g" as the command argument and "some_file" as the file argument.

Solution for your problem: write

Code:
sed "$toReplace" "$outputFile" > "${outputFile}.tmp"

to be on the save side. Generally: quote enthusiastically - always! ;-)) In programming it never hurts to be overly pedantic.

I hope this helps.

bakunin
# 4  
Old 09-17-2008
Many thanks to the both you.

In particular, found the example very useful and have definitely learned a thing or two. I'll be wearing my double quoting hat from now on!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Arduino-cli - Uploading to Unknown Chinese Arduino Boards using the Arduino Command Line Interface

In my further exploration of Arduino, today I decided to install the arduino-cli on my mac today. https://github.com/arduino/arduino-cli I followed the instructions for macOS but when I got to this part: arduino-cli board list I got the dreaded "Unknown" Fully Qualified Board Name... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Unknown command '\;'

Hello, I am running ubuntu 16.04 . Below script gives: "unknown command '\;' counter=105 while read COL1 COL2 COL3 do let counter++ url="http://localhost/test/$COL1.$COL2.$COL3"; mysql -uroot -D mydatabase -pmypasswd -e "SELECT video_id FROM video_series_files WHERE id IN \ (SELECT... (12 Replies)
Discussion started by: baris35
12 Replies

3. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

4. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

5. Shell Programming and Scripting

There's a unknown command on line 1 and i don't know whats causing it.

My OS is Linux GL version 2.4.26. I am trying to make a shell script that outputs a chart displaying info of who is online, boot time, and the real time, I have manage to get it to work, but I still get the error "./user/script: line 1: : command not found." I do in fact have access to root, if... (2 Replies)
Discussion started by: thatguy565
2 Replies

6. HP-UX

'SP2-0734: unknown command beginning "elect' Error

My environment is HP-UX... NIS CEL | uname -a HP-UX s53kj113 B.11.11 U 9000/800 1510201964 unlimited-user license I ran this command directly from the server's sqllplus. Logically, it should have work. But in my case, it always escaped the 1st character of the next command after the... (3 Replies)
Discussion started by: aimy
3 Replies

7. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

8. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

9. Solaris

PING - Unknown host 127.0.0.1, Unknown host localhost - Solaris 10

Hello, I have a problem - I created a chrooted jail for one user. When I'm logged in as root, everything work fine, but when I'm logged in as a chrooted user - I have many problems: 1. When I execute the command ping, I get weird results: bash-3.00$ usr/sbin/ping localhost ... (4 Replies)
Discussion started by: Przemek
4 Replies

10. Solaris

<drive type unknown> when format command run

Hi, we are running solaris 9 on V280R. Due to power failure, one disk's label and lost its details like drive type etc. Disk details: <SUN72G cyl 14087 alt 2 hd 24 sec 424> single partition on slice 6 with UFS. The below i see when i run "format" command ..... ..... 4.... (1 Reply)
Discussion started by: prvnrk
1 Replies
Login or Register to Ask a Question