LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-30-2017, 03:20 AM   #1
raysa
LQ Newbie
 
Registered: Mar 2017
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 03-30-2017, 03:47 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
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.
 
Old 03-30-2017, 03:48 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
What does "ls *txt" return ?.
 
Old 03-30-2017, 03:56 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
would be nice to see exactly what have you tried, what's happened, what was expected....
 
Old 03-30-2017, 08:26 AM   #5
raysa
LQ Newbie
 
Registered: Mar 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-30-2017, 09:21 AM   #6
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by raysa View Post
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).
 
Old 04-01-2017, 04:18 AM   #7
raysa
LQ Newbie
 
Registered: Mar 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-01-2017, 05:30 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
d'oh - thanks for letting us know ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] SED Guru Needed -- Combine Two SED Commands Into One Liner RandyTech Linux - Newbie 4 03-23-2017 08:03 AM
LXer: GNU Grep & Sed: Fallout Within The GNU FSF Camp LXer Syndicated Linux News 0 12-24-2012 05:39 PM
sed and regexp matching (GNU sed version 4.2.1) Ashkhan Programming 8 02-27-2012 09:12 AM
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
how do other commands work with 'sed' sumitdevbharadwaj Linux - General 2 07-29-2009 09:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration