The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem with output of find command being input to basename command... new_learner UNIX for Dummies Questions & Answers 2 12-14-2008 02:56 AM
awk/sed Command : Parse parameter file / send the lines to the ksh export command rajan_san Shell Programming and Scripting 4 11-06-2008 01:29 PM
assign a command line argument and a unix command to awk variables sweta_doshi Shell Programming and Scripting 0 08-08-2008 06:54 AM
Help Required: Command to find IP address and command executed of a user loggedout Security 2 08-06-2008 08:12 PM
inconsistent ls command display at the command prompt & running as a cron job rajranibl SuSE 5 07-30-2007 08:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-19-2009
sakthifire sakthifire is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 26
Awk command

Hi
I have a input file like the below:

V1^a,b
V1^a
V2^b,c
V1^a,b,c
V2^c,d
V3^e,a,f

I want the output file like the below.

V1^a
V1^b

V1^a
V2^b
V2^c

V1^a
V1^b
V1^c

V2^c
V2^d

v3^e
V3^a
V3^f


I want the output file to be shown using awk command .

Regards
Sakthifire
  #2 (permalink)  
Old 03-19-2009
Shahul's Avatar
Shahul Shahul is offline
Registered User
  
 

Join Date: Aug 2008
Location: India
Posts: 175
hi,

Code:
$ cat inputfile |tr '^' ','|awk -F, 'OFS="^" {if(NF>2) {{print $1,$2}  {print $1,$3} {print $1,$4}} else {print $1,$2}}'|awk -F"^" '{if($2 != "") print }'
V1^a
V1^b
V1^a
V2^b
V2^c
V1^a
V1^b
V1^c
V2^c
V2^d
V3^e
V3^a
V3^f
Thanks
Sha
  #3 (permalink)  
Old 03-20-2009
sakthifire sakthifire is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 26
Thank you Sha ...
Its working.

Sakthifire
  #4 (permalink)  
Old 03-19-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Code:
awk -F^ '{n = split( $2,a,",")
for ( x = 1; x <= n; ++x ) printf "%s^%s\n", $1, a[x]
}' FILENAME
  #5 (permalink)  
Old 03-19-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Another approach:

Code:
awk 'BEGIN{ FS="[,^]" } { for(i=2;i<=NF;i++) print $1"^"$i }' file
Regards
  #6 (permalink)  
Old 03-19-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Code:
nawk -F"^" '{
	num=split($2,arr,",")
	for(i=1;i<=num;i++)
		print $1"^"arr[i]
}' filename
Closed Thread

Bookmarks

Tags
awk function

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0