LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-24-2012, 04:39 AM   #1
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Rep: Reputation: Disabled
Question find one word in a line


can someone help me with a command to find any one word lines in a file and copy those lines in a new file.. for example :-

The file is a file

There
are 90 boys in the hall.
[tab][space] some

of them are into sports. some are in
class-of-blue.

THE OUTPUT SHOULD BE


There
[tab][space] some
class-of-blue.


[tab][space] are actual tab or space. not the words.
 
Old 10-24-2012, 04:50 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434

Rep: Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790
Sounds a bit like homework (https://www.linuxquestions.org/linux/rules.html); can you show us what you've tried so far?

Here's some good links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 10-24-2012, 04:55 AM   #3
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
i have tried grep command so far.

Quote:
grep '^[ \t]*[A-Za-z0-9_]\+\.\?' file > new file
 
Old 10-24-2012, 05:30 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
sed -r '/[[:alnum:]]+[^[:alnum:]]+[[:alnum:]]+/d' oldfile > newfile
the character class "alnum" includes all letters and numbers, so this works only if other characters are not present

also try:
Code:
sed -r '/[^[:blank:]]+[[:blank:]]+[^[:blank:]]+/d' oldfile > newfile
 
1 members found this post helpful.
Old 10-24-2012, 06:29 AM   #5
sam_sung
LQ Newbie
 
Registered: Oct 2012
Posts: 27

Original Poster
Rep: Reputation: Disabled
that worked.. thanks mate.
 
Old 10-25-2012, 04:29 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

They will also allow you to insert literal tabs and spaces into the text.

Here's a more compact grep for you (actually egrep, since it needs extended regex):

Code:
grep -E '^\s*(\w|[[:punct:]])+\s*$'

\s is a built-in synonym for [[:space:]], and \w is a synonym for [[:alnum:]_].

(a|b) is a regex either-or grouping, and finally * means "0 or more" and + is "1 or more".

So this line means any amount of whitespace, followed by a string of one or more word characters and/or punctuation characters, followed by more optional whitespace.

See the regular expressions section of info grep for more on how character classes and backslash characters are defined.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find specific word in a line in a file. Kashif_Bash Programming 5 04-20-2012 06:15 PM
[SOLVED] Find position of specific word in a line rajeshpvndd Linux - Newbie 3 11-05-2011 07:57 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM
How to find occurance of word in one line atul_sp Linux - Server 5 07-21-2008 01:20 PM
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 01:57 PM

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

All times are GMT -5. The time now is 09:42 PM.

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