SED to delete last word on line...what's wrong?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED to delete last word on line...what's wrong?
# 1  
Old 08-10-2010
SED to delete last word on line...what's wrong?

I have a line that gets pulled from a database that has a variable number of fields, fields can also be of a variable size. Each field has a variable number of spaces between them so there is no 'defined' delimiter. The LastData block is always a single word.

What I want to do is delete the last word (LastData) on each line....

example line:-

Some Data More Data Specific Formatted Data EvenMore Data Bit More Data LastData EOL

The space after the LastData and EOL is deliberate to show how the data can come up.

So I have a sed as follows...

sed -e 's/ *$//' -e '/[ ][^ ]*$//' (Please note there IS a space (s) before the first (s)* and inbetween the [(s)] and also [^(s)]).

The first sed deletes any trailing spaces at the end of the line between 'LastData' and EOL and this works correcly.

However the second sed is deleting 'LastData' and the last word in 'Bit More Data' as well.

Could someone please explain to me where I'm going wrong, please don't just give an example that will work without explaining it else I won't learn from my misunderstanding.

Thanks in advance.
# 2  
Old 08-10-2010
I've used your sed command with two different versions of sed (gnu and AT&T AST's) and both work just fine.

Before looking at your try, I wrote one and did things just a bit differently:

Code:
# newlines only make it more readable
sed   's/[ \t]*$//; 
s/[ \t][^ \t]+$//; 
s/[ \t]*$//;'

The first subs command ditches trailing whitespace upto the end of line. The second, ditches the last word, and a preceding whitespace character. The third ditches any trailing whitespace that might have been left. The biggest difference is that it catches both tabs and spaces. If "data" and "lastdata" are separated by a tab, and not a space, your substitutions will see <space>data<tab>lastdata as the last 'word.'

Don't know if this is what is actually going on, but it's the only way I could explain it.
# 3  
Old 08-10-2010
Thanks agama

I found the problem, the final two fields had a tab rather then a space between them, only instance on the line and I hadn't checked for it.

Problem solved.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed script to delete the last word after a last pattern match

Hi Guys , I am having a file as stated below File 1 sa0 -- i_core/i_core_apb/i_afe0_controller/U261/A sa0 -- i_core/i_core_apb/i_afe0_controller/U265/Z sa1 -- i_core/i_core_apb/i_afe0_controller/U265/A sa1 -- i_core/i_core_apb/i_afe0_controller/U268/Z sa1 -- ... (7 Replies)
Discussion started by: kshitij
7 Replies

2. UNIX for Dummies Questions & Answers

How to delete a particular word on particular line with sed?

I have a file from which I am trying to delete a particular word on a particular line. NEW NEW /v/engine NEW /ifs/list NEW /ifs/vdrome NEW I am trying to delete the first line only if it contains the word NEW. I am also trying to delete the last line only if it contains the word NEW. I... (11 Replies)
Discussion started by: newbie2010
11 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. UNIX for Dummies Questions & Answers

delete last word of each line a directory

I want to delete the last word of each line in all the files in one directory but dont know what I am doing wrong FILES="data/*" for X in $FILES do name=$(basename $X) sed s/'\w*$'// $X > no-last/${name} done Can you please help me :wall: (8 Replies)
Discussion started by: A-V
8 Replies

5. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

6. Shell Programming and Scripting

delete line containin specified word

write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.help is appreciated .thank you. (2 Replies)
Discussion started by: shawz
2 Replies

7. UNIX for Advanced & Expert Users

Delete a word and complete line

Hi Canone please provide me solution how can achieve the result below: File1.txt $ sweet appleŁ1 scotish green $ This is a test1 $ sweet mangoŁ2 asia yellow $ This is a test 2 $ sweet apple red (there is no pound symbol here) germany green (1 Reply)
Discussion started by: Aejaz
1 Replies

8. Shell Programming and Scripting

What's wrong with this sed command? delete & append

I want to write a sed command that does the following work: file: <a>asdfasdf<\s> <line>hello</line> <b>adf<\c> <b>tttttttt<\c> output: name=hello sed -e 's/^*//' -n -e '/<line>/s/<*>//gp;' -e 's/^/name="/g' file but I can not append "=" after getting the line with... (5 Replies)
Discussion started by: minifish
5 Replies

9. UNIX for Dummies Questions & Answers

Find a word and delete the line

Hi I have a text file like this name today.txt the request has been accepted the scan is successful at following time there are no invalid packages 5169378 : map : Permission Denied the request has been accepted Now what i want do is I want to search the today.txt file and if i... (1 Reply)
Discussion started by: gsusarla
1 Replies

10. Shell Programming and Scripting

Sed help to delete everything prior to WORD

How would I delete everything on a line in a file prior to a specific word? In other words, I have a file that contains the word SEARCH on various lines and would like to delete everything prior to SEARCH on all lines. Thanks for that help (2 Replies)
Discussion started by: drheams
2 Replies
Login or Register to Ask a Question