LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2013, 06:14 AM   #1
georgi
LQ Newbie
 
Registered: Apr 2012
Location: Bulgaria
Distribution: openSuSE
Posts: 15

Rep: Reputation: Disabled
Delete lines with "|" and numbers from 0-10 at the end


Hi everybody,

I have a large file with some lines ending with "|" and numbers from 0-10 at the end and I want to delete them. Example
Code:
...|9
I have used successfully "sed" to delete all having numbers 0-9, but can not figure out how to also include 10. The command I use is

Code:
sed -i.bkup -e 's/|[0-9]//g' filename

Any help is greatly appreciated.
 
Old 09-30-2013, 06:29 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Code:
sed -i.bkup -e 's/|[0-9][0-9]*$//g' filename
Also, do you want to delete the lines that match this or remove “|<number>” from the end of those lines? Your example command does the latter, for the former use either of:
Code:
sed -i.bak -e '/|[0-9][0-9]*$/d' filename
Code:
grep -v '|[0-9][0-9]*$' <filename >filename.new
 
1 members found this post helpful.
Old 09-30-2013, 06:38 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,141

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
That will remove all two digit numbers as well, not just "|10".

Personally I would just add "-e '/s/|10$'//'
 
1 members found this post helpful.
Old 09-30-2013, 07:05 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
you can use grouping:
sed -e 's/|\([0-9]\|10\)$/d'
 
Old 09-30-2013, 08:22 AM   #5
georgi
LQ Newbie
 
Registered: Apr 2012
Location: Bulgaria
Distribution: openSuSE
Posts: 15

Original Poster
Rep: Reputation: Disabled
mina86, your first code works fine
Code:
sed -i.bkup -e 's/|[0-9][0-9]*$//g' filename
I just want to delete “|<number>” not the entire line.

syg00, adding your suggestion does not work

pan64, your suggestion comes with an error
Code:
sed: -e expression #1, char 19: unterminated `s' command
thanks everyone for the time and efforts. Appreciate your help
 
Old 10-01-2013, 12:22 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,950

Rep: Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326Reputation: 7326
sorry, this one should work:
sed -e 's/|\([0-9]\|10\)$//g'
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
how to delete the lines containing "./." linuxon Linux - Newbie 6 01-25-2012 06:26 AM
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 06:41 PM
[SOLVED] "Insert" & "Delete" key returns "~" in a terminal. sharky Linux - General 15 04-26-2011 08:36 AM
fdisk reports odd "Start "and "End" sectors on single partition eponymous Linux - Software 3 10-01-2007 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 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