Selecting awk output depending on grep result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting awk output depending on grep result
# 1  
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
# 2  
Old 06-18-2012
That is a useless use of cat.

So you want to ignore lines with too few fields?

Code:
awk -F '"' 'NF>8 {print $8 " " $12 " " $16}' file1

Ah, you want to carry it forward?

Code:
awk -F '"' 'NF>8 {print $8, $12, $16; A=$8; B=$12; next}; { print A, B, $4 }' file1

# 3  
Old 06-18-2012
If there are 19 fields (NF==19) then record $8 and $12 in variables, say v1 and v2 and print $8,$12,$16 . If NF==9 then print v1, v2 and $4.

Last edited by Scrutinizer; 06-18-2012 at 04:45 PM.. Reason: clarified it a little bit..
# 4  
Old 06-18-2012
Sorry for not being clear. It is not the number of fields that matters in each line. I added the line numbers below so that they can be easily referred. They are not part of the file content.

There are basically two formats. One format contains the user's last/first names and UID, e.g. lines 1, 2, 3 and 6. The second format contains only the UID (that is useful), e.g. lines 4 and 5. The second format appears when the user in the above line has more than one UID's in the system. So in the result of the script, I want that user name repeated with additional UID's rather than being omitted (as in the original file). Does this make sense?

I also did not make it clear that in the lines in the second format, the UID's always appear in the same field, $4, e.g. lines 4 and 5. So I want to repeat $8 and $12 from the directly above line (line 3), and print $4 for lines 4 and 5.

If saying $4 is a number is not ideal, $5 in the second format always contains the exact same string, blah in this case in lines 4 and 5.

1 "pippo","is fat","last","Cruise","first","Tom","UID","1234","is 50 years old"
2 "monky","looks funky","last","Jones","first","Catherine Zeta","UID","2345","is still hot"
3 "lion","rules savannah","last","Baldwin","first","Alec","UID","3456","his brother sued costner and lost"
4 "taco","4567","blah","age of rock"
5 "chili","5678","blah","flopped bit time"
6 "mojito","tastes awesome","last","Brand","first","Russell","UID","6789","didn't deserve katy"
# 5  
Old 06-18-2012
Why do the number of fields not matter? line 1,2,3 and 6 have 19 fields and 4 an 5 have 9 fields, why can that not be used a s criterium?
# 6  
Old 06-18-2012
OK, I see what you are saying. The only reason is that I don't know if there will be yet another format that has a different number of fields. But I can cross the bridge when I come to it. The only issue with the suggested script is that it grabs the fields that I don't want for line 5. It grabs $8 and $12 from line 4. I want to repeat $8 and $12 from line 3.

Some user may have a dozen of UID's. So lines 4 and 5 can be a dozen of lines and all of them belong to the user in line 3. Then it proceeds with users with only one UID for a while, hit another user with multiple UID's, and so on.

Thanks for great advices in such short time,
# 7  
Old 06-18-2012
If I put #3 into script I get this:
Code:
awk -F\" 'NF==19{a=$8; b=$12; print a,b,$16} NF==9{print a,b,$4}' infile

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question