LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-07-2008, 02:23 PM   #1
aolong
Member
 
Registered: Aug 2006
Location: US
Distribution: Debian, CentOS
Posts: 40

Rep: Reputation: 15
SED, regexp or such - remove text after space


I have a long list of names in mysql field 'first':

Andrew H.
Mary
John T.
etc...

And a lot of fields in 'last' like:

Huges, MD
Rowe, DDS, CPR
Bonner
etc...

I am trying to clean this up to remove everything after the first space in 'first' and everything after and including the comma in 'last'.

I have failed with mysql regexp and thought to export the names and try sed. Can anyone help with this?

Andrew
 
Old 03-07-2008, 02:29 PM   #2
Wells
Member
 
Registered: Nov 2004
Location: Florida, USA
Distribution: Debian, Redhat
Posts: 417

Rep: Reputation: 53
Well, if you have them exported it is probably easier to use awk on them:

Code:
$ cat firstnames_list.txt | awk '{print $1}' > new_firstnames_list.txt
$ cat lastnames_list.txt | awk -F , '{print $1}' > new_lastnames_list.txt
The "-F ," portion in the second one tells awk to split on commas instead of on spaces, which is the default. You might have to run it through a couple of these filters to get rid of all of it.

If you wanted to print the second (or third, or whatever) item that is split by the token you have chosen (spaces, commas, whatever) just change the $1 to $2 or $3 or whatever number you want.
 
Old 03-07-2008, 02:30 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926Reputation: 926
What does the exported file look like? Can't make a guess
as to a valid regex if I don't know the input.


Cheers,
Tink
 
Old 03-07-2008, 02:32 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680Reputation: 680
Are these two lists or are you showing to types of patterns from one list?
sed 's/ .*//' filename
sed 's/, .*//' filename2

Or if from the same list you can have two sed commands on one line:
sed 's/ .*//;s/, .*//' filename
Another form is the -e option:
sed -e 's/ .*//' -e 's/, .*//' filename
 
Old 03-07-2008, 02:36 PM   #5
aolong
Member
 
Registered: Aug 2006
Location: US
Distribution: Debian, CentOS
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Wells View Post
Well, if you have them exported it is probably easier to use awk on them:

Code:
$ cat firstnames_list.txt | awk '{print $1}' > new_firstnames_list.txt
$ cat lastnames_list.txt | awk -F , '{print $1}' > new_lastnames_list.txt
The "-F ," portion in the second one tells awk to split on commas instead of on spaces, which is the default. You might have to run it through a couple of these filters to get rid of all of it.

If you wanted to print the second (or third, or whatever) item that is split by the token you have chosen (spaces, commas, whatever) just change the $1 to $2 or $3 or whatever number you want.
Man, that was fast and it works like a charm!

Thanks!
 
Old 03-07-2008, 02:36 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
If you indeed have the data the way you show it, it is easy:

For the "first" list---remove all after the " ":

sed -i 's/ .*//' filename (deletes everything from the first space to the end of the line the regex reads literally as "a space, followed by any number of characters")

A similar approach will work for the second case....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with sed / regexp elinenbe Programming 2 02-01-2008 10:09 AM
bash/sed/awk fill each line in text file with space to fixed length khairil Programming 11 01-09-2008 05:28 AM
sed: delete text till <pattern2> depending on length of text oyarsamoh Programming 2 05-05-2007 01:40 AM
help with sed to remove all text except for some Benanzo Linux - Software 7 01-04-2007 06:21 AM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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