Lingua Franca - step 16

Hi all.

Step 16 doesn’t seem to work for me on MacOS. Information I found online seems inconclusive but the fault seems to be MacOS specific. Any idea what causes the problem here?

Thanks

lingua-franca👽$ sed -i 's/Lingua-Franca/Lingua Franca/g' */*.txt
sed: 1: "africa/afrihili.txt": command a expects \ followed by text
lingua-franca👽$ grep -Rl 'Lingua-Franca' */*.txt | wc -l
       6
lingua-franca👽$ 

Edit

The following version worked. I skipped -i and it worked.

sed 's/Lingua-Franca/Lingua Franca/g' */*.txt

1 Like

Yea different systems have slightly different procedures. You can always type man sed or man anything for that matter to see what your specific system documentation is.

1 Like

Great tip about man command. Thanks!

1 Like

I find this command works on MacoS (without the -i) but it dosnt save the files, it just shows the output. If i look in the files after this command then i dont see them as updated.
So on MacOS you need to run the following command to update the files with the sed command:

sed -i ‘’ ‘s/Lingua-Franca/Lingua Franca/g’ <filename(s)>.

I’m still confused by this and cant seem to stop getting sed: 1: “africa/afrihili.txt”: command a expects \ followed by text

what filename are you putting after after sed -i ‘’ ‘s/Lingua-Franca/Lingua Franca/g’ ?

thanks

I found the solution in this stackoverflow Q&A. Hope it helps!
https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux`

2 Likes

perfect thank you so much

1 Like

I experienced the same issue on the mac. Thanks to your link, now I use Perl to replace sed. But I still don’t understand why Perl did the same command. I searched what Perl is but the results saying Perl is a language are not helping. Could you explain? Thanks in advance.

The issue as mentioned before is that OSX appears to use a different version of sed than the course is based on (you can find quite a lot of discussion about this with a web search). From the link above it appears mac uses BSD sed whereas the course guidance is based on GNU sed. As per the first reply using man on your own system to check the manual for your version of sed would be the best option and a good habit to get into.

Perl is indeed it’s own language. If you wanted a little information about using it with the flags you did then the following link seems reasonable- https://www.perl.com/pub/2004/08/09/commandline.html/
For more complete details (which you probably don’t need unless you’re very curious and have some free time) you can always check the docs- https://perldoc.perl.org/

2 Likes

Very much appreciate your detailed explanation! I will check the links you sent.

1 Like

I’m on a mac using terminal and been having the same issue. I had to add \ after the -i like so:

sed -i \ ‘s/Lingua-Franca/Lingua Franca/g’ /.txt

This worked for me…don’t ask me how :confused:

I am stuck at the same place with the same problem. I have used man and ascertained it’s using BSD. I’ve tried all the adjustments and still stuck.

Command runs OK and checks everything like this:

sed 's/Lingua-Franca/Lingua Franca/g' */*.txt 

but doesn’t catch all instances. Once I add the -i option:

sed -i 's/Lingua-Franca/Lingua Franca/g' */*.txt 

I get this error:

sed: 1: "africa/afrikaans.txt": command a expects \ followed by text

I tried it just with Europe folder, thinking it was something to do with the ‘a’. But

sed -i 's/Lingua-Franca/Lingua Franca/g' europe/*.txt

yields:
sed: 1: "europe/english.txt": invalid command code e

I think it’s something to do with the /.txt part and how BSD interprets that. I used the following link and had success by adding in --posix option.

sed -i --posix 's/Lingua-Franca/Lingua Franca/g' */*.txt 

BSD/macOS Sed vs. GNU Sed vs. the POSIX Sed specification

Would love to know why that broke, and why that worked. But would really love it if it was mentioned in the tutorial!

1 Like

Check the man page for the -i flag specifically on OSX. It should mention a required extension be passed to this option (it’s intended to create a backup rather than edit in place, e.g. -i .bak). I believe it’s treating 's/Lingua-Franca/Lingua Franca/g' as the extension and then gets confused by what it thinks is the next command.

As per a few other comments (and hopefully the man page) you can pass a zero-length extension '' to effectively edit the files in place (though it comes with a mild warning about doing so).

That’s helpful, ta. I thought the .bak info in the StackExchange was specific to that example. I’ll have another look in the sed manual and have another crack at this now.

Thank you so much. This saved my life :smiley:

sed -i --posix 's/Lingua-Franca/Lingua Franca/g' */*.txt 

appears to have worked - despite creating a .txt–posix file for every .txt file in the directory. I went ahead and deleted these using : rm */*.txt--posix

I was just about to add that the command used to check the result wasn’t working - then I realised that the wc option was -l not -1!!

Thanks for the BSD/GNU sed article.

I had the same issue on my mac.

sed -i’’ -e ‘s/Lingua-Franca/Lingua Franca/g’ /.txt
This worked for me.

sed -i'' -e ... - does not work on OS X 10.6 but works on 10.9+

Ok so I’m on an m1 Mac OSX version Big sur 11.2.3 (2021)

The lesson code “sed -i ‘s/Lingua-Franca/Lingua Franca/g’ /.txt” didn’t work.

If found out this code is the solution on Mac OSX:

sed -i ‘’ ‘s/Lingua-Franca/Lingua Franca/g’ /.txt

note the quote mark after sed -i looks like it is a double quote, but is in fact two singles ’ ’ with no space in between.

When using the check command grep -Rl ‘Lingua-Franca’ /.txt | wc -l this returns a 0 indicating success!

I found this out by googling link here: https://unix.stackexchange.com/questions/272040/using-sed-to-find-and-replace-a-string-inside-a-file

Hope that helps

Found some more info on this in case you’re curious… there are different implementations so using sed to modify strings is different in different situations. So;

We use -i to select in-place editing on the $file file. In some systems it is required to add suffix after -i flag which will be used to create backup of original file. You can add empty string like -i ‘’ to omit the backup creation. Source