Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-05-2007, 12:41 AM   #16
LQ Guru
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled

Look up grep in the man pages.
Old 01-06-2007, 06:34 PM   #17
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678Reputation: 678
for line in `cat -n $file`; do
grep 'one'
If you don't supply an argument to grep, it takes its input from the standard input.

You could replace this loop with the single command: grep -n 'one' $file

If grep didn't have the -n option, as maybe the original version of grep, then you could use:
cat -n $file | grep one

You are still thinking like you are programming in basic instead of using the tools as filters.
At work, I produced a couple one liners that would catalog DVD backups and create a csv file of contents and disc label names. Another oneliner merges and sorts the csv files ( actually I changed it to a tab seperated file ) and uses enscript to produce a nice looking postscript file. Then I use ps2pdf to convert the file to a pdf which I share on the server. Most of this is done using ls, sed, sort and uniq. Simple text based filters. By piping the output of one to the other and tweaking the arguments till they get right, I was able to create the scripts referencing the man pages and applying trial and error. I didn't have to use the "read" command once.

Also consider this construct:
for file in *.jpg; do

This is a loop but uses wild cards to set up the loop.

Another common way of doing the same thing is used when the number of files is too great:
find ./ -maxdepth 1 -iname "*.jpg" -print0 | xargs -0 -L 1000 ...

This uses the NULL character to separate arguments and pipes the list to xargs which can limit how many arguments are handled at one time.

After I back up files before deleting them using K3B, I will save the k3b project file. It is a zipped file containing a "maindata.xml" xml file that contains the files backed up.
I will use sed to extract the filenames to delete. Then I'll pipe that through "tr" to convert return characters to NULLs and then pipe that to "xargs -0 rm" to remove the files. By piping the output of "sed" through "tr" I am able to produce the same text stream that "find -print0" would produce and use xargs the same way.

I would highly recommend installing the source packages for coreutils, sed, and awk. Not to install these tools you already have, but to produce pdf versions of the manuals from the .texi source that these source packages supply.

Last edited by jschiwal; 01-06-2007 at 06:54 PM.


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
shell program, how to find substring plz madhugp Programming 1 12-29-2006 11:43 AM
how to find an exact substring match? ldp Programming 7 02-22-2005 07:28 AM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 04:01 PM
Python - Set vars and loop over lines in file jnoller Programming 6 02-07-2004 11:32 AM
Loop over lines in text file? amaze Linux - General 2 08-12-2003 08:15 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:28 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration