LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-06-2007, 12:43 AM   #1
ryanlum
Member
 
Registered: Jul 2007
Posts: 47

Rep: Reputation: 15
Extracting Information from a Flat file


hey guyz,

I have this situation where.. i have a flat file. I will have to sum up only a certain field in the flat file.

Is there a way i can grep out the following format? Its a 1 line format.. but there are tons of lines.. so i would need to loop through all the lines

format:
1&1&1&1&1&3&1&1&1&1
1&2&2&2&2&3&2&1&1&1

I would like to grep out that particular section with the number 3 only in order to add it together.

Whats the best and easiest way to do this?
 
Old 11-06-2007, 01:00 AM   #2
poet_imp
LQ Newbie
 
Registered: Nov 2007
Location: Tucson, AZ
Distribution: Kubuntu/Gutsy
Posts: 26

Rep: Reputation: 15
Most of the time I can figure out what is being asked. This time I am unable to. Can you try to explain again?
 
Old 11-06-2007, 01:05 AM   #3
ryanlum
Member
 
Registered: Jul 2007
Posts: 47

Original Poster
Rep: Reputation: 15
i have a file with the pattern above.
its seperated by & and i want to extract it from a specific column after the &

is that cleaR?
 
Old 11-06-2007, 01:08 AM   #4
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
( cool http://www.grymoire.com/Unix/Awk.html )
 
Old 11-06-2007, 01:35 AM   #5
ryanlum
Member
 
Registered: Jul 2007
Posts: 47

Original Poster
Rep: Reputation: 15
I tried awk....
put for that format it doesn't do anything..

is there a way to count the number of & in the string, then take the next one?
 
Old 11-06-2007, 01:36 AM   #6
ryanlum
Member
 
Registered: Jul 2007
Posts: 47

Original Poster
Rep: Reputation: 15
becuase there are times when the format is like this

1&1&&2&
 
Old 11-06-2007, 02:29 AM   #7
lakris
Member
 
Registered: Sep 2004
Location: Stockholm, Sweden
Distribution: Ubuntu, RedHat, SuSe, Debian, Slax
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by ryanlum View Post
becuase there are times when the format is like this

1&1&&2&
Could this help? In bash...

lakris@Lakrix:~/Projects/scripts$ cat source.txt
1&1&1&1&1&3&1&1&1&1
1&2&2&2&2&3&2&1&1&1
1&2&7&2&2&3&2&1&1&1
1&2&2&2&2&3&2&1&1&1
1&2&9&2&2&3&2&1&1&1
1&2&&2&2&3&2&1&1&1

lakris@Lakrix:~/Projects/scripts$ val=0;sum=0;while read line;do val=`echo $line|cut -d"&" -f3`; [ x$val == x ] && val=0; sum=$(($sum+$val)); done < source.txt; echo $sum
21
lakris@Lakrix:~/Projects/scripts$

It reads the file, extracts the third &-separated field and accumulates it, and prints the sum.

/Lakris

Last edited by lakris; 11-06-2007 at 02:37 AM.
 
Old 11-06-2007, 02:30 AM   #8
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by ryanlum View Post
hey guyz,

I have this situation where.. i have a flat file. I will have to sum up only a certain field in the flat file.

Is there a way i can grep out the following format? Its a 1 line format.. but there are tons of lines.. so i would need to loop through all the lines

format:
1&1&1&1&1&3&1&1&1&1
1&2&2&2&2&3&2&1&1&1

I would like to grep out that particular section with the number 3 only in order to add it together.

Whats the best and easiest way to do this?
i don't quite follow your question

an example of how the output should appear would be great
but this will count number of & in README.txt

grep -o '&' README.txt | wc -l | sed s/\ //g
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Packet Information exl75 Linux - Networking 15 04-03-2007 02:59 PM
Extracting Information from Web pagges opto General 3 09-25-2006 02:19 AM
Database vs. Flat File pdeman2 Programming 32 04-16-2006 11:38 PM
Extracting terminal/console information Woodsman Slackware 6 11-12-2005 12:34 PM
Making VSAM file from a flat file in AIX Raghothaman AIX 2 08-25-2004 04:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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