awk not generating the expected output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk not generating the expected output
# 1  
Old 09-14-2010
awk not generating the expected output

Hi,

I am presently stuck in a csv file.
Code:
 
INPUT CSV
 
baseball,NULL,8798765,Most played
baseball,NULL,8928192,Most played
baseball,NULL,5678945,Most played
cricket,NOTNULL,125782,Usually played
cricket,NOTNULL,678921,Usually played

EXPECTED OUTPUT CSV
 
baseball,NULL,8798765,Most played 
           NULL,8928192,Most played   
           NULL,8928231,Most played  
cricket,NOTNULL,125782,Usually played
         NOTNULL,678921,Usually played

I am using the below code in input csv

Code:
 
awk -F"," '{if (NR>1 && a==$1) {print FS$2FS$3;a=$1}  else {print $0;a=$1}}' input.csv > output.csv

But I am getting the below output:-

Code:
 
baseball,NULL,8798765,Most played 
              8928192   
              8928231
cricket,NOTNULL,125782,Usually played
                678921

The code is deleting the duplicate items in 2nd column and third columns also...I want that the unique elements of the first column should be displayed but all other things for other columns should remain intact

Can somebody please help me...
# 2  
Old 09-14-2010
Code:
awk  'BEGIN{FS=OFS=","}!a[$1] {print;a[$1]++;next}{print "\t"$2,$3,$4}' infile

baseball,NULL,8798765,Most played
        NULL,8928192,Most played
        NULL,5678945,Most played
cricket,NOTNULL,125782,Usually played
        NOTNULL,678921,Usually played

# 3  
Old 09-14-2010
Hi,
It is giving the below error:-
awk 'BEGIN{FS=OFS=","}!a[$1] {print;a[$1]++;next}{print "\t"$2,$3,$4}' input.csv

awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 09-14-2010
Quote:
Originally Posted by scripter12
Hi,
It is giving the below error:-
awk 'BEGIN{FS=OFS=","}!a[$1] {print;a[$1]++;next}{print "\t"$2,$3,$4}' input.csv

awk: syntax error near line 1
awk: bailing out near line 1
Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 09-14-2010
It worked...
Thanks very much..
Can you please explain it, so that it can be useful to me...
and also I can make some modification if it is necessary...
# 6  
Old 09-14-2010
Quote:
Originally Posted by scripter12
It worked...
Thanks very much..
Can you please explain it, so that it can be useful to me...
and also I can make some modification if it is necessary...
Ok, I'll explain the code of rdcwayx.
Code:
BEGIN{FS=OFS=","}

Set fieldseparators
Code:
!a[$1] {print;a[$1]++;next}

If the 1st field doesn't exist as an index of array a:
- print the whole line
- create a new element with the new index (1st field)
- increase the value
- get the next line.
Code:
{print "\t"$2,$3,$4}

This line should be run if the first field exists as an index in array a.
Print a tab and the fields 2, 3 and 4

Last edited by Franklin52; 09-14-2010 at 03:10 PM.. Reason: Misspelling
This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 09-14-2010
Thanks for the explanation Franklin.It was really of great help

From the last two days, I am seeing you guys are solving the issues like any thing else. I am overwhelmed by the quick and thorough response.
How can you do that ?
You don't know, how helpful you guys are to many of us.

Once again thanks very much. Looking for your support in future.

And also let me know how to give thanks, so that it comes in the profile of the solution provider...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk not giving the output expected

Hello, I am practising awk and decided to compare two columns and print the result of the comparison as third column i/p data c1,c2,c3 1,a,b 1,b,b i am trying to compare the last two columns and if they match I am trying to print match else mismatch(Ideally i want that as a last column... (5 Replies)
Discussion started by: mkathi
5 Replies

2. UNIX for Beginners Questions & Answers

Split Command Generating Incomplete Output Files

Hello All, May i please know how do i ensure my split command would NOT generate incomplete output files like below, the last lines in each file is missing some columns or last line is complete. split -b 50GB File File_ File_aa |551|70210203|xxxxxxx|12/22/2010 20:44:58|11/01/2010... (1 Reply)
Discussion started by: Ariean
1 Replies

3. Shell Programming and Scripting

For loop not giving expected output

#cat /tmp/input old_array old_dev new_dev new_array 0577 008AB 01744 0125 0577 008AC 01745 0125 0577 008AD 005C8 0125 0577 008AE 005C9 0125 0577 008AF 005CA 0125 0577 008B0 005CB 0125 0577 008B1 005CC 0125 cat test.sh #!/bin/ksh... (4 Replies)
Discussion started by: mbak
4 Replies

4. Shell Programming and Scripting

awk output not what was expected

Good Moring, I am currently reading about awk in a manual and following the examples using the oratab file. My system is SOLARIS 10 I think I am getting strange behavior judging by what the book says to do and what I am getting with my little program. Here is my program: grep -v oratab |... (4 Replies)
Discussion started by: bdby
4 Replies

5. Shell Programming and Scripting

Grep can't match expected but output all

lyang001@lyang001-OptiPlex-9010:~$ service --status-all |grep dbus acpid acpi-support alsa-restore alsa-store anacron apport atd avahi-daemon bluetooth cgroup-lite console-setup cron cups dbus dmesg dns-clean failsafe-x ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

6. Shell Programming and Scripting

Randomly selecting sequences and generating specific output files

I have two files containing hundreds of different sequences with the same Identifiers (ID-001, ID-002, etc.,), something like this: Infile1: ID-001 ATGGGAGCGGGGGCGTCTGCCTTGAGGGGAGAGAAGCTAGATACA ID-002 ATGGGAGCGGGGGCGTCTGTTTTGAGGGGAGAGAAGCTAGATACA ID-003... (18 Replies)
Discussion started by: Xterra
18 Replies

7. Shell Programming and Scripting

Not getting expected output

Hi I have written below script to get the data in table form. #!/bin/sh echo "File Name\tType" for i in *; do echo "$i\t\c" if ; then echo "directory" elif ; then echo "symbolic link" elif ; then echo "file" else echo "unknown" fi donehowever i am getting output in different way... (3 Replies)
Discussion started by: scriptor
3 Replies

8. Shell Programming and Scripting

Output is not comming as expected

Hi All, I am in middle of one script. I want output in the form of xls file. There are 4 fields - user name, email Id, full name, date of birth. I want these details to get in seperate columns. But, i am getting it in the single cell and as like a paragraph.:mad: Please suggest me some... (8 Replies)
Discussion started by: Agupte
8 Replies

9. Web Development

Detecting browser locales/languages and generating output

We have a Java app that renders Localized text on user's browser session based upon browser language settings. The app reads the browser language settings and prepares the localized text. But recently we faced issues for Mozilla 5.0 version browser. Note our code works fine in IE. Taking an... (1 Reply)
Discussion started by: uunniixx
1 Replies

10. Shell Programming and Scripting

need help generating this output

need to check hardware error are zero iostat -en |awk '{ if ( $2 == 0 ) { print " " } else { print " Hardware errors "} } can someone please tell me whats wrong with this ---------- Post updated at 10:19 PM ---------- Previous update was at 10:16 PM ---------- iostat -en ----... (11 Replies)
Discussion started by: arch12
11 Replies
Login or Register to Ask a Question