Can't get sed commands to work (GNU sed 4.2.2 in Ubuntu 16.04.2)
I have a set of text files in a folder and I want to make changes to them all at once.
From a terminal in that folder I've tried to remove all blank lines with: sed '/./!d' *txt I've also tried removing a specific line with: sed '17d' *txt But the files remain unchanged. Having read lots (but understanding little) about sed, I've tried the same commands with sed -i in front and also sed -e but that didn't make any difference. I don't get any error messages in the terminal, but the action just doesn't happen. What am I missing? Thanks. |
Welcome.
You won't get errors unless an error happens. If sed successfully does what you told it to do then it will not say anything. Before you try affecting the files in-place, it is good to make a backup with tar or something. Code:
tar zcf txt.backup.tar.gz *txt Code:
sed -e '/^$/d' *txt If that works then you can add the -i to modify the files in-place, if you have GNU Sed. |
What does "ls *txt" return ?.
|
would be nice to see exactly what have you tried, what's happened, what was expected....
|
Thanks for these three replies. Yes. I'm very careful about backups and the files I'm working on are dispensable.
I have a folder with several text tiles. If I type ls .txt in a terminal in this folder I get the list of files. All the files have several unwanted blank lines in them (no blank spaces - completely empty, just the CR). The start of each file is identical and I'd like to remove line 17 from them all. So, in a terminal opened in this folder, I have typed various example commands (separately, one command at a time) that I've seen on sed guide pages. To remove the blank lines I've tried: sed '/./!d' *txt sed -e '/./!d' *txt sed -i '/./!d' *txt sed '/^$/d' *txt sed -e '/^$/d' *txt sed -i '/^$/d' *txt To remove line 17 I've tried: sed '17d' *txt sed -e '17d' *txt sed -i '17d' *txt In each case, the folder and all the files in it have remained completely unchanged. My system is Ubuntu 16.04.2 with GNU sed 4.2.2 I'd love to get sed working for this task (it's as job which will often recur), but I can't see what I'm doing wrong. Thanks for any help. |
Quote:
Are the files writable? That is, does the command "ls -l" show "-rw-r--r--" or similar? Try editing one file with an editor and see if you can save it. You can use "chmod u+w *.txt" to make them writeable if they're not (assuming you own them and the drive is writable). |
After looking at the files in more detail, and doing some experimenting, I discovered what the problem was: The text files I was trying to edit with sed had CR line-end characters.
When I changed this to LF the sed commands worked fine. Thank you to those who made suggestions. |
d'oh - thanks for letting us know ... :p
|
All times are GMT -5. The time now is 07:43 PM. |