LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Can't get sed commands to work (GNU sed 4.2.2 in Ubuntu 16.04.2) (https://www.linuxquestions.org/questions/linux-software-2/cant-get-sed-commands-to-work-gnu-sed-4-2-2-in-ubuntu-16-04-2-a-4175602828/)

raysa 03-30-2017 03:20 AM

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.

Turbocapitalist 03-30-2017 03:47 AM

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
Then you can try your hand at sed. You would use the pattern /^$/ to find any lines that are completely empty and the command 'd' to delete those found.

Code:

sed -e '/^$/d' *txt
The caret ^ stands for the start of the line and the dollar sign $ stands for the end of the line. If the line might have white space on it, then you could modify the pattern using the character class for white space /^[[:space:]*$/d

If that works then you can add the -i to modify the files in-place, if you have GNU Sed.

syg00 03-30-2017 03:48 AM

What does "ls *txt" return ?.

pan64 03-30-2017 03:56 AM

would be nice to see exactly what have you tried, what's happened, what was expected....

raysa 03-30-2017 08:26 AM

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.

KenJackson 03-30-2017 09:21 AM

Quote:

Originally Posted by raysa (Post 5690197)
But the files remain unchanged.

If you didn't use -i, the output will only scroll to the screen. You can either redirect it to a new file (sed ... > newfile.txt) or use -i to modify it in place.

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).

raysa 04-01-2017 04:18 AM

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.

syg00 04-01-2017 05:30 AM

d'oh - thanks for letting us know ... :p


All times are GMT -5. The time now is 07:43 PM.