Split and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split and print
# 1  
Old 10-27-2008
Split and print

I have a file with data such as:


X Y Z 4 1,3,5,7, 4,6,8,10,
A B C 3 2,3,4, 5,9,11,
E F G 5 1,2,3,4,5, 8,9,10,11,12,


Columns 1, 2 and 3 are descriptions. Column 4 tells how many numbers are in columns 5 and 6

What I'd like to do is split column 5 and column 6 by the "," and then print out a line with columns 1, 2, 3, 4, and the 1st number from column 5 and column 6, then print out a line with columns 1, 2, 3, 4,and the 2nd number in cols 5 and 6 ... until all the numbers in 5 and 6 are printed.

X Y Z 4 1 4
X Y Z 4 3 6
X Y Z 4 5 8
X Y Z 4 7 10

A B C 3 2 5
A B C 3 3 9
A B C 3 4 11

E F G 5 1 8
E F G 5 2 9
E F G 5 3 10
E F G 5 4 11
E F G 5 5 12



I was trying something like:

awk '{
split($5,a,",");
split($6,b",");
for (i=1; i<= $4; i++)
print $1,$2,$3, a[i], b[i]
}’
infile > outfile &


This isn't working

Thank you so much.

edit:

%$#%# I just messed up the syntax it works if I can use the commas correctly

split($5,a, ",");
split($6,b, ",");

Sorry
# 2  
Old 10-27-2008
Really, it works fine on my computer.
Code:
awk '{
        split($5, a, ",");
        split($6, b, ",");
        for(i=1; i<= $4; i++)
                print $1,$2,$3, $4, a[i], b[i]
}' sli.t

Code:
X Y Z 4 1 4
X Y Z 4 3 6
X Y Z 4 5 8
X Y Z 4 7 10
A B C 3 2 5
A B C 3 3 9
A B C 3 4 11
E F G 5 1 8
E F G 5 2 9
E F G 5 3 10
E F G 5 4 11
E F G 5 5 12

# 3  
Old 10-27-2008
Thank you. Those pesky commas get me every time. Smilie
# 4  
Old 10-28-2008
awk:
Code:
sed 's/, /,/g' file > file.temp
nawk '{
n=split($5,arr,",")
for(i=1;i<=$4;i++)
	print $1" "$2" "$3" "$4" "arr[i]" "arr[i+$4]
}' file.temp
rm file.temp

perl:
Code:
open FH,"<file";
while(<FH>){
	$_=~s/, /,/g;
	$_=~tr/\n//d;
	my @arr=split;
	my @temp=split(",",$arr[4]);
	for ($i=0;$i<$arr[3];$i++){
		print "$arr[0] $arr[1] $arr[2] $arr[3] $temp[$i] $temp[$i+$arr[3]]\n";
	}
}
close FH;


Last edited by summer_cherry; 10-28-2008 at 01:13 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

2. Shell Programming and Scripting

Awk, split, print

How to print the split array elements in the same line with awk? echo "1 2 3 4 /path/to/file1" | awk 'split($5, A, "/") {print $0; for (i=1; i<=length(A); i++) print A}' echo "2 2 3 6 /longer/path/to/another/file2" | awk 'split($5, A, "/") {print $0; for (i=1; i<=length(A); i++) print A}' What... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Solaris

Setting up Solaris 10 to print to a windows print server

Guys, I have a issue that I am trying to rectify please advise. lpstat -t shows scheduler is running printer lext644 disabled since Mon Dec 02 19:48:18 2013. available.I have restarted the printer service and it shows online but the above says disabled. I have a lot of jobs in the print... (1 Reply)
Discussion started by: terrywhitejr
1 Replies

4. UNIX for Dummies Questions & Answers

How to create a print filter that print text & image?

Currently, I have a print filter that takes a text file, that convert it into PCL which then gets to a HP printer. This works. Now I need to embedded a image file within the text file. I'm able to convert the image file into PCL and I can cat both files together to into a single document... (1 Reply)
Discussion started by: chedlee88-1
1 Replies

5. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

6. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

7. AIX

split a filename and print to 2 different headings

I need help to split a filename 'a0crk_user:A0-B0123$#%test' into a0crk_user and A0-B0123 and print the output under 2 different columns namely User and Status. for eg. the output should like below: User Status ---- ------ a0crk_user A0-B0123 (3 Replies)
Discussion started by: mbak
3 Replies

8. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

9. HP-UX

Print Problem in UNIX. Need to know the option to specify the print paper size

Hi, Could any one please let me know what is the option available in UNIX to print by specifying the paper size? We are using Unix11i. I could n't see any option specified in the 'lp' command to print the report by specifying the size of the paper. It would be of great help to me, if... (1 Reply)
Discussion started by: ukarthik
1 Replies

10. Shell Programming and Scripting

split and print $PATH

Hello simple question : how can i split the $PATH by the ":" seperator with one liner ? mybe using awk is there any builtin function in awk that splits and prints the output ? thanks (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question