views:

61

answers:

3

I have some problem with replace command.

Input file i have this

{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}

I want to replace with single quotes

'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'

Using the below mentioned command

find input.txt -exec sed 's/{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}/'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'/g' {} \;

I am getting error like this

bash: syntax error near unexpected token `('
A: 

It's because the naked ' in your replacement string is actually terminating the sed command, meaning the shell is trying to process the line. This actually became immediately obvious when Benoit's edit caused your question to syntax-colour correctly. You can see the second ' on the line (the first character of the substitution text) changed the colour from red to black due to the string termination.

In addition, sed won't like the use of naked [] since they indicate character classes in regular expressions.

You can fix both problems with:

pax> find input.txt -exec sed 's/{a\[$1\]=a\[$1\]FS$2}END{for(i in a) print i,a\[i\]}/\x27{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}\x27/g' {} \;

which outputs:

'{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'

So basically, escape the square brackets in the search string so they're treated as literals and use \x27 in the replacement string instead of '.

paxdiablo
Pax thanks for you reply. It is not working. Output comes as Input
gyrous
@gyrous, try again with the updated command, there's a better way than using double quotes with even more escapes :-) It uses a hex code to insert the replacement single quotes, meaning less escapes. If you're getting the same output as input then probably either your input string isn't as specified or the `sed` command has been mistyped. It works fine under GNU sed 4.1.5.
paxdiablo
No pax. it print x27 instead of '
gyrous
@gyrous, I suggest you type in `sed --version` and let us know what version you're running. As I stated, this works fine with my GNU `sed`. If you're using a different `sed`, that could be the cause.
paxdiablo
as i type sed --version as you said. it says illegal option
gyrous
Then you're running a deficient sed. If you are on Linux, then you should be running the GNU variant which has supported `--version` for as long as I can remember. I suggest you check to make sure the right `sed` is first in the path. If you're not on Linux, then your sed is deficient compared to GNU's and you may want to let us know which platform it is (i.e., change the tags).
paxdiablo
schot
+1  A: 

Hello.

First, you cannot insert easily a literal single quote in an argument that is delimited with single quotes.
You have to replace your ''s with '"'"' or '\''.
As a hint to guess this problem, here, the complaint comes from the shell, which means that even your command-line is malformed.

Second, you should be aware that dollar and parentheses are special characters in sed regular expressions. You will probably have to escape them. Read man sed for more details on this.

Third, I am not sure whether find input.txt will work. I guess you meant find . -type f -name input.txt?

Benoit
Interestingly enough, that `find` does work. It treats the name as the path so is really no different to `sed 'blah blah' input.txt`.
paxdiablo
A: 

I find it funny why you should have to do this. You should write the correct code in the first place, since I can see its awk code. Don't tell me you purposely omit the single quote in your input.txt file when you created it? And its only a single file you are editing. There's no need to use find. (unless you don't know where it is).

ghostdog74
Ghostdog: Your correct but this is my problem
gyrous
awk -F, 'BEGIN{OFS=","} {print("awk -F, '{a[$1]=a[$1]FS$2}END{for(i in a) print i,a[i]}'"$0" >> /export/home/minsat/batch/OFFLINE_OFFERS/WRONG/TOTAL.txt")}' /export/home/minsat/batch/OFFLINE_OFFERS/WRONG/TMP.txt Tmp.txt some file names are there. i want to prepare command line one by one
gyrous
If i run this script, because of '{a single quotes it brings error bash: syntax error near unexpected token `('. So first i prepare command file wilthout single quotes
gyrous
after that i am trying to add sigle quotes
gyrous
show what you are doing in your question where you can do proper formatting!.
ghostdog74