LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-11-2009, 01:46 PM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Rep: Reputation: 23
using sed or grep to extract stuff from a text file


I need to get some information out of a text file. The information is of the following format, for example:

# TOKEN: some.label, /folder/folder1/etc..., /folder/folder2/etc..., 2

There may be many of these lines in the text file. Finding the lines is easy as all begin with # TOKEN: followed by four comma separated values: a label, a folder1, a folder2, a numeric.

How would I extract each line and then process each line in turn using the comma separated values in a bash script? Perhaps using sed or grep and which would be better?

pseudo example:

read textfile
for each line in textfile
if line contains "# TOKEN:"
process(line.get(label), line.get(folder1), line.get(folder2), line.get(numeric))
end if
end loop

Obviously using grep/sed this logic may differ, but you get the idea of the function required.

Thanks

Last edited by DEF.; 12-11-2009 at 01:47 PM.
 
Old 12-11-2009, 02:24 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by DEF. View Post
I need to get some information out of a text file. The information is of the following format, for example:

# TOKEN: some.label, /folder/folder1/etc..., /folder/folder2/etc..., 2

There may be many of these lines in the text file. Finding the lines is easy as all begin with # TOKEN: followed by four comma separated values: a label, a folder1, a folder2, a numeric.

How would I extract each line and then process each line in turn using the comma separated values in a bash script? Perhaps using sed or grep and which would be better?
Actually, this is a task that awk is ideally suited for: the basic unit of computation in awk is a field. By default, fields are white-space separated, but they can be comma separated. I would google around for some basic awk documentation, and see what you can't learn that way.
 
Old 12-11-2009, 02:45 PM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
You can do all that in awk if you like.

Try:
http://www.grymoire.com/Unix/Awk.html
 
Old 12-11-2009, 07:18 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
with bash , you can do something like this
Code:
declare -a save
while read -r line
do
    IFS=","
    case "$line" in
    *#*TOKEN*)
        set -- $line
        # save $@
        save=($@)
        for i in "${save[@]}"
        do
            echo "-->"$i
        done
    esac
done <"file"
or you can just use awk
Code:
awk -F"," '/#.*TOKEN.*/{print "first field is: "$1}'  file
I don't recommend sed/grep to parse csv files.

Last edited by ghostdog74; 12-11-2009 at 07:21 PM.
 
1 members found this post helpful.
Old 12-12-2009, 09:57 AM   #5
DEF.
Member
 
Registered: Apr 2009
Posts: 96

Original Poster
Rep: Reputation: 23
Thanks all. I have never used AWK, but given your advise I will.
 
Old 12-12-2009, 10:13 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by DEF. View Post
Thanks all. I have never used AWK, but given your advise I will.
In terms of power gained for work done learning it, awk is the best language I have ever learned -- way more intuitive than shellscript or PERL for example. It may have helped that I had once known C.

Here are some awk links
 
  


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
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
[Grep,Awk,Sed]Parsing text between XML tags. ////// Programming 5 07-26-2011 11:54 AM
sed/grep new lines in text hk20 Linux - Newbie 2 09-13-2008 06:47 PM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM
Simple Text Reformat (sed and grep) mikefazz Programming 2 08-31-2004 07:38 PM

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

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