LinuxQuestions.org
Review your favorite Linux distribution.
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 04-30-2010, 06:57 AM   #1
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Rep: Reputation: 0
how to get the specific text from a txt file in bash script


Hi,

I have a text file which stores the list of files & dir, I want to get only file's extensions from this file & want to store it in another file.

eg, below is the file's contents & from it I want to get the extensions sh, pl & h & want to store it in another file. Also I don't want directory list.

A scripts/services_restarter.sh
A scripts/svn post_commit scripts
A scripts/tmp/
A scripts/tmp/as
A scripts/tmp/update.h
A scripts/tmp/vmware.sh
A scripts/tuning-primer.sh
A scripts/dbupdate.pl
A scripts/vmware.sh


Can somebody help ?

Last edited by deepakdeore2004; 04-30-2010 at 07:03 AM.
 
Old 04-30-2010, 07:03 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Something like this?

egrep ".sh$|.pl$|.h$" infile > outfile

Hope this helps.
 
Old 04-30-2010, 07:05 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
I don't quite understand. You want only the extensions? Or only files with certain extensions? I'm guessing the latter.

If so, one word: "grep".
Code:
grep ".sh\|.pl\|.h" oldfile.txt > newfile.txt
edit: Beaten by Druuna.

Last edited by David the H.; 04-30-2010 at 07:09 AM.
 
Old 04-30-2010, 07:10 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,497

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
I think the OP might be looking for something a bit more generic - say
Code:
 egrep -o '\.[[:alpha:]]+$' infile
Depending on requirements maybe pipe it to sort then uniq.
 
Old 04-30-2010, 07:16 AM   #5
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Original Poster
Rep: Reputation: 0
Hi David,

Thanks for the quicker reply, actually I want to get only the extensions & there may be other extensions as well in the input file.

eg. new file will contain only files' extensions, i.e.
pl
sh
h
any_other_extension

my goal is to create a svn pre-commit hook which will not allow the file check-ins other than the specific extensions.

Thanks,
Deepak
 
Old 04-30-2010, 07:19 AM   #6
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
awk -F"." '/\./ { print $NF}' text.txt
 
Old 04-30-2010, 07:29 AM   #7
deepakdeore2004
LQ Newbie
 
Registered: Apr 2010
Posts: 26

Original Poster
Rep: Reputation: 0
Thumbs up Thanks syg00

Thanks syg00,

It worked exactly what I wanted !

Hi PMP,

It is also working but It is giving the the extension of the directory also, eg. it is giving me the extension of dir "csrbkp.pnq.net/" also.


anyways ....

becoming lazy .... can anybody please provide pre-commit script which will allow only specific file check-ins :-)
 
Old 04-30-2010, 07:33 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
This will get the unique ones:
Code:
awk -F. '!_[$NF]++{print $NF}' text
Includes the empty extension as well
 
Old 04-30-2010, 07:35 AM   #9
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Modified

Code:
awk -F"." '/\.[^/]+$/ { print $NF}' text.txt
 
  


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
Bash script to compare numbers in a txt file leopard86 Programming 6 09-11-2012 01:10 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM
bash script to apply sed command only to a specific text area mauran Programming 6 07-13-2007 05:38 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
bash-script: output text between two ocurrences of a specific string isl01jbe Programming 1 06-17-2004 03:36 PM


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