• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

sed Command to match words at beginning and end of line.

How would I remove lines that start and end with the same word, using sed?

Something from: sed ‘s/^ .* $//g'

0
vupadhya
Asked:
vupadhya
1 Solution
 
tfewsterCommented:
awk '$1 != $NF {print $0}' yourfile
(you may need to set the input field seperator with the -F option to awk)
0
 
vupadhyaAuthor Commented:
sed?
0
 
ahoffmannCommented:
cannot be done with a single sed, but you can try to pipe a few cryptic sed, not worth the effort if there is a simple awk solution ;-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
koppchaCommented:
Try This

sed -e 's/\([a-zA-Z]*\) .*\1$//g' sample.txt > newFile.txt
0
 
koppchaCommented:
notice there is a space after ([a-zA-Z]*\)
([a-zA-Z]*\)space.*\1$//g
0
 
koppchaCommented:
sorry forgot ^ at the starting .Please try this and let me know

sed -e 's/^\([a-zA-Z]*\) .*\1$//g' sample.txt > newFile.txt
0
 
ahoffmannCommented:
hmm, sounds like Gnu's sed is not that stupid ;-) thanks koppcha
as in question "remove lines" I'd use

sed -e '/^\([a-zA-Z]*\) .*\1$/d'  file
0
 
koppchaCommented:
yeah i am about to add that comment with 'd' so that it will eliminate the lines .thanks
0
 
koppchaCommented:
Ahoffmann
    I started with UNIX like 8 months ago...i usually keep track of your answers they are amazing.Glad we have you here. :)
0
 
ahoffmannCommented:
<off-topic>
koppcha .. "started with UNIX like 8 months ago...i usually keep track of your answers .."
you see that I'm still learning too at EE ;-)
keep in mind that my answers are simple and short usually, and they are a bit picky (see Gnu example above), sometimes ...
</off-topic>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now