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 s |
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? |
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! |
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 |
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. |