LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 08-15-2010, 12:58 PM   #1
wasup
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Help with a bash script


Awesome forum!

I am trying to figure out a way to pull http links out of text files and then output the results in a log. The text files are in folders like this inside a source directory.

/source
./folder1
...folder1.txt
./folder2
...folder1.txt
./folder3
...folder1.txt

So basically I would like to get the output to look something like this.

folder1
http://example.com

folder2
http://example.com

folder3
http://example.com

I just can't wrap my head around how to do this.

Thanks in advance
 
Old 08-15-2010, 01:25 PM   #2
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
Sounds like a possible homework question, so just some basic advice for the moment.

Break it down into the steps you need to perform. Figure out how to do each one individually, then you can combine them at the end.

First you need to compile a list of filenames. Take a look at the find command.

Next, you need to figure out how to extract the links you need from each file. This depends on the exact format of the text, but the usual tools are grep, sed, or awk.

Finally, create a loop to process each file in the list and output the desired format to your log file.
 
1 members found this post helpful.
Old 08-16-2010, 11:59 PM   #3
wasup
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Original Poster
Rep: Reputation: 0
David thanks for the help.

Give the folder names:
ls ~/folder

Would give the path of the text files:
find ~/folder -name *txt

This would extract the http link out of the txt files:
grep "http://" /folder/name.txt | sed 's/^.*http:/http:/' | sed 's/\s.*$//' | sort

The thing that stumps me is how to do the loops.

The output I am looking for would be...

folder
http://www.link.com

folder1
http://www.link.com

etc
 
Old 08-17-2010, 12:07 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Hey wassup

Quote:
ls ~/folder
There are a few potential issues with using ls (although possibly not here). The one issue I can see though is that your ls will
make no distinction between files and directories, so assuming you only want directories it will fail.
Quote:
find ~/folder -name *txt
This is a better start and if you run it on the command line you will see that you also get the folder names (hint)
Quote:
grep "http://" /folder/name.txt | sed 's/^.*http:/http:/' | sed 's/\s.*$//' | sort
Here I will be a little harsher and say why??? Now I realise you are newish so the reason for the why is because sed can search just like grep can
so it is a waste to have it in there. Also, maybe you could show us a before and after of the line you want as the sed's seem quite over the top as well.
Quote:
The thing that stumps me is how to do the loops.
Lastly, go to your favorite source for bash (I like this one) and look up either a for or while loop
 
Old 08-17-2010, 01:12 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Hello. Try this one.
Code:
cd ~/folder; find -type f -iname '*txt' -exec grep -o "http://[^[:blank:]\"']\+" {} \;
 
  


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
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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