Sponsored Content
Top Forums Shell Programming and Scripting Selecting awk output depending on grep result Post 302658007 by takada on Monday 18th of June 2012 02:57:56 PM
Old 06-18-2012
Selecting awk output depending on grep result

Hi,

I don't script often enough to know how to do this, and I can't seem to find a right example online. I have a csv output from an old, old system (Win2K???), from which I want to extract only certain fields. Initially I came up with something like this:

Code:
cat file1 | awk -F '"' '{print $8 " " $12 " " $16}'

The csv file contains some data like this:

<file1>
Code:
"pippo","is fat","last","Cruise","first","Tom","UID","1234","more blah"
"monky","looks funky","last","Jones","first","Catherine Zeta","UID","2345","more blah"
"lion","rules savannah","last","Baldwin","first","Alec","UID","3456","more blah"

So the output would be:

Code:
Cruise Tom 1234
Jones Catherine Zeta 2345
Baldwin Alec 3456

But I realized later that the csv file may contain lines that do not conform to the above format when a user has more than one UID. The additional UID's are appended following the first appearance to that user name like this:

Code:
"pippo","is fat","last","Cruise","first","Tom","UID","1234","is 50 years old"
"monky","looks funky","last","Jones","first","Catherine Zeta","UID","2345","is still hot"
"lion","rules savannah","last","Baldwin","first","Alec","UID","3456","his brother sued costner and lost"
"taco","4567","blah","age of rock"
"chili","5678","blah","flopped bit time"
"mojito","tastes awesome","last","Brand","first","Russell","UID","6789","didn't deserve katy"

I am trying to script it so that I will get an output like this:

Code:
Cruise Tom 1234
Jones Catherine Zeta 2345
Baldwin Alec 3456
Baldwin Alec 4567
Baldwin Alec 5678
Brand Russell 6789

I would think I can do this with if statement and while loop? Users with multiple UID's can appear several times randomly, but one user with multiple UID's appear as a sequential block and one time only in the initial csv file.

If someone can point me to the right direction, I would greatly appreciate it.

Regards,

Bash Noob...

Last edited by Scrutinizer; 06-18-2012 at 04:03 PM.. Reason: code tags
 

10 More Discussions You Might Find Interesting

1. HP-UX

awk to output cmd result

I was wondering if it was possible to tell awk to print the output of a command in the print. .... | awk '{print $0}' I would like it to print the date right before $0, so something like (this doesn't work though) .... | awk '{print date $0}' (4 Replies)
Discussion started by: IMTheNachoMan
4 Replies

2. Shell Programming and Scripting

Selecting patterns from the results of grep

Hi All, I'm struggling with a problem that I'm wondering (and hoping!) that someone can help me with. I have a number of .xml files which I'm using grep to search for the string 'include'. I need to extract the value of the include from the grep result. For example, on any given file, I... (2 Replies)
Discussion started by: Steve_altius
2 Replies

3. UNIX for Dummies Questions & Answers

exit status depending on output

Hi I have a script that does a select from a table. I want the script to return a 0 status if a specific column has 'OK' value or return 1 if this column returns 'NOT OK'. The output now is similar to: COL1 COL2 STATUS ------- --------- ------- I want to be limited to... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

4. UNIX for Dummies Questions & Answers

Bash - CLI - grep - Passing result to grep through pipe

Hello. I want to get all modules which are loaded and which name are exactly 2 characters long and not more than 2 characters and begin with "nv" lsmod | (e)grep '^nv???????????? I want to get all modules which are loaded and which name begin with "nv" and are 2 to 7 characters long ... (1 Reply)
Discussion started by: jcdole
1 Replies

5. Shell Programming and Scripting

Redirect output to a different text file depending source of data

I have a list of DNS servers I need to look up information on. Each of these servers has a master and a slave database. Essentially what I need to do is create two text files for each server. One with the Master view and one with the Slave view. There's 20 servers, in the end I should have 40 text... (4 Replies)
Discussion started by: spartan22
4 Replies

6. Shell Programming and Scripting

Output block of lines in a file based on grep result

Hi I would appreciate your help with this. I have a output file from a command. It is broken based on initial of the users. Exmaple of iitials MN & SS. Under each section there is information pertaining to the user however each section can have different number of lines. MY challenge is to ... (5 Replies)
Discussion started by: mnassiri
5 Replies

7. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

8. Shell Programming and Scripting

Use bash command on awk field and output the result

Hello, I want to run a field from an awk command through a command in bash. For example my input file is 1,2,3 20,30,40 60,70,80 I want tot run $2 thought the command date +%d/%m/%y -d"01/01/15 + $2 days -1 day" and get the output 1,02/01/15,3 20,30/01/15,40 60,11/03/15,80 ... (2 Replies)
Discussion started by: garethsays
2 Replies

9. Shell Programming and Scripting

Compare 2 columns from the same file and print a value depending on the result

Hello Unix gurus, I have a file with this format (example values): label1 1 0 label2 1 0 label3 0.4 0.6 label4 0.5 0.5 label5 0.1 0.9 label6 0.9 0.1 in which: column 1 is a row label column 2 and 3 are values I would like to do a simple operation on this table and get the... (8 Replies)
Discussion started by: ksennin
8 Replies

10. Shell Programming and Scripting

Grep output to file result not as expected

Hi Gurus, I run command grep ABC file1 > file2 against below file. I got all ABC_xxx in one line in file2. I expect to get multiple lines in file2. If I print result in screen, the result is expected. thanks in advance My os is SunOS 5.10 Generic_150400-64 sun4v sparc sun4v ABC_123 XXXXX... (2 Replies)
Discussion started by: green_k
2 Replies
All times are GMT -4. The time now is 09:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy