LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2016, 10:45 AM   #1
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Rep: Reputation: Disabled
List arrange on terminal


I have a big list like this (before the names there are empty spaces):

Nicolaes Tulp (1593–1674), physician, surgeon and mayor of Amsterdam
Gustav de Vries (1866–1934), mathematician
Hugo de Vries (1848–1937), geneticist

I would like to cut only names.
So the list should be:

Nicolaes Tulp
Gustav de Vries
Hugo de Vries


How could I do that?
I tried
Code:
tr -d ' \t' <inputfile>outputfile
But the result was:
NicolaesTulp
GustavdeVries
HugodeVries

Thank you in advance.
 
Old 11-25-2016, 11:04 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Hi,

How about trying:
Code:
 cut -d '(' -f1 <inputfile> > <outputfile>
What this does is "cuts" fields (-f) from the input file and puts them to stdout. You redirect to a file using the ">". -d specifies the delimiter, which in this case is the first parenthesis '(' and you wish only the first field prior to that delimiter.

Example:
Code:
$ cat 1.txt
Nicolaes Tulp (1593–1674), physician, surgeon and mayor of Amsterdam
Gustav de Vries (1866–1934), mathematician
Hugo de Vries (1848–1937), geneticist
$
$ cut -d ' ' -f1-2 1.txt
Nicolaes Tulp
Gustav de
Hugo de

Last edited by rtmistler; 11-25-2016 at 11:05 AM.
 
1 members found this post helpful.
Old 11-25-2016, 11:13 AM   #3
crazy-yiuf
Member
 
Registered: Nov 2015
Distribution: Debian Sid
Posts: 119

Rep: Reputation: 51
You can also use sed to match everything up to the parenthesis.
Code:
sed 's/\(^[a-zA-Z ]*\)(.*/\1/1' list.txt
 
1 members found this post helpful.
Old 11-25-2016, 11:14 AM   #4
freeroute
Member
 
Registered: Jul 2016
Location: Hungary
Distribution: Debian
Posts: 69

Original Poster
Rep: Reputation: Disabled
@rtmistler
Thanks your advise. Working.
Almost good. I have 5 space characters before every name in my list and I would like to cut these, too. It is not shown my original post.
 
Old 11-25-2016, 11:27 AM   #5
crazy-yiuf
Member
 
Registered: Nov 2015
Distribution: Debian Sid
Posts: 119

Rep: Reputation: 51
Then you can do:
Code:
sed 's/^[ ]*\([a-zA-Z ]*\)(.*/\1/1' list.txt
Sed's a pain, but extremely useful. Especially if you're a programmer and need to refactor array[0][1] to getArray(0,1), or something like that.
 
1 members found this post helpful.
Old 11-25-2016, 01:00 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by freeroute View Post
@rtmistler
Thanks your advise. Working.
Almost good. I have 5 space characters before every name in my list and I would like to cut these, too. It is not shown my original post.
Quote:
Originally Posted by crazy-yiuf View Post
Then you can do:
Code:
sed 's/^[ ]*\([a-zA-Z ]*\)(.*/\1/1' list.txt
Sed's a pain, but extremely useful. Especially if you're a programmer and need to refactor array[0][1] to getArray(0,1), or something like that.
crazy-yiuf has it correct; however in the event that you encounter a TAB as opposed to spaces, you may wish to include a search for TAB in the regex:
Code:
 sed 's/^[ \t]*\([a-zA-Z ]*\)(.*/\1/1' list.txt
 
1 members found this post helpful.
Old 11-25-2016, 01:24 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
and you can do it in two steps, like:
Code:
sed 's/^[ \t]*//; s/[ \t]*(.*//'
the first one will remove the spaces before, the second one will remove ( and everything afterward. But remember it may not work properly if you have more than one (.
 
1 members found this post helpful.
  


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] Command to arrange vertical list in a horizontal manner shivaa Linux - Newbie 9 07-28-2016 05:59 PM
Auto open and arrange terminal and editor on login richman1234 Linux - Newbie 2 07-05-2013 05:22 PM
[SOLVED] How do you use the terminal to list different drives? nitzix Linux - Newbie 5 06-11-2012 10:11 PM
[SOLVED] rename and arrange files based on a list babak1112 Linux - General 2 01-29-2012 08:47 AM
Terminal : How to list previous commands? concoran Linux - Newbie 6 07-12-2011 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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