LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 09-21-2012, 11:06 AM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Rep: Reputation: 1
Grabbing field and out putting to pg


Real easy question but for some reason escapes me.

I am tring to cut out data from a file (f2,5). I want only the data where f2 begins with a 9. I want the field I cut tab delimited and printed out on my screen.

Here is the command I wrote:
Code:
cat <filename> | grep ^9 | cut -f2,5 -d | pg
Any help would be greatly appreciated.

s
 
Old 09-21-2012, 11:48 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Might be an idea to show some test data?

Also cat is not required as grep reads files.

Also, you did not mention what was wrong with what you tried?
 
Old 09-21-2012, 12:49 PM   #3
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
Here is some sample data:
X|8|951000000000020120908|9|9510000000|20120908|


This is what the command executed:

Code:
cat <filename> | grep ^9 | cut -f2,5 -d "/" | pg

This is its output:
9|2|L|41500000000001|20120821|300.00|1|41000000000111

I know where my problem is I just do not know how to resolve it. It obviously is grabbing every line that begins with a '9'. However, I need it to grab all lines that begin with a '9' in field 2. Not to mention it is not delimiting it the way I wrote it. I tried to delimit by default <tab>, but it asks for parameters so I added "/". It still | delimited the output. Plus, I should only have 2 columns field 2 and 5. Man am I a mess!
 
Old 09-22-2012, 01:04 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Right ... I am a little confused, but this is what I am thinking you are meaning:

1. Find all lines that start with a 9 in the second field (fields delimited by pipe (|))

2. From lines found, extract the second and fifth fields and print them with a tab in between

3. Pass this output to pg

So assuming the above is correct, I would try something like:
Code:
awk -F"|" '$2 ~ /^9/{print $2"\t"$5}' filename | pg
If the above assumptions are incorrect, please advise and I will have another go?
 
Old 09-24-2012, 02:14 PM   #5
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
It worked, thank you. I was trying to avoid awk, but I now see the need to learn awk better. The code is much more robust and elegant.

s
 
  


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
[SOLVED] Grabbing the last field of a delimited entry without using "awk" or "sed" drandre Linux - Newbie 17 04-18-2012 10:54 PM
[SOLVED] Count occurrence of character in field and print in a new field Trd300 Linux - Newbie 5 03-21-2012 07:57 PM
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
awk printing from Nth field to last field sebelk Programming 2 01-08-2010 09:39 AM
php question, how do I get a return from a field within a field? cherrington Programming 11 04-29-2009 01:27 AM


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