LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grabbing field and out putting to pg (https://www.linuxquestions.org/questions/linux-newbie-8/grabbing-field-and-out-putting-to-pg-4175428401/)

smturner1 09-21-2012 11:06 AM

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

grail 09-21-2012 11:48 AM

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?

smturner1 09-21-2012 12:49 PM

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!

grail 09-22-2012 01:04 AM

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?

smturner1 09-24-2012 02:14 PM

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


All times are GMT -5. The time now is 11:25 AM.