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, 05: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, 05:50 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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, 05: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, 06:30 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 07: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, 05:29 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 07:15 PM
[SOLVED] Find position of specific word in a line rajeshpvndd Linux - Newbie 3 11-05-2011 08:57 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 07:35 AM
How to find occurance of word in one line atul_sp Linux - Server 5 07-21-2008 02:20 PM
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 02:57 PM


All times are GMT -5. The time now is 06: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration