formatting a file headache


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers formatting a file headache
# 8  
Old 02-16-2007
Hi Anbu
Thanks for this lovely looking awk array
awk -F"," ' {
str=$1
for( i = 2 ; i <= NF - 7 ; ++i )
str=str "," $i
n=split($(NF-6),arr,":")
for( i = 1 ; i <= n ; ++i )
print str "," arr[n]
} ' file[/INDENT]
Thanks but unfortunately this doesn't work & I'm too much of a newbie to fix it. It nearly works but rather than putting the next course on each line, it just repeats the same course. Output is:

User Name,Password,ID,First Name,Last Name,Subject
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
glik2398,xxxxxx,123456789,Gill,Likely,PHYS4597
glik2398,xxxxxx,123456789,Gill,Likely,PHYS4597

etc etc

What I want is output that looks like
User Name,Password,ID,First Name,Last Name,Subject
jsmi3279,xxxxxx,123456789,John,Smith,XMLT1005
jsmi3279,xxxxxx,123456789,John,Smith,MATH2987
jsmi3279,xxxxxx,123456789,John,Smith,PHYS4597
jsmi3279,xxxxxx,123456789,John,Smith,FRNC3578
jsmi3279,xxxxxx,123456789,John,Smith,ITLN590
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
glik2398,xxxxxx,123456789,Gill,Likely,PHYS4597
glik2398,xxxxxx,123456789,Gill,Likely,,XMLT1001
glik2398,xxxxxx,123456789,Gill,Likely,XMLT1004
glik2398,xxxxxx,123456789,Gill,Likely,MATH2987


from input that looks like this (the input in a file hundreds of lines long - this is just the first few lines)

User Name,Password,ID,First Name,Last Name,Subject,Other,#User Type,City,#Email,#Some Field,#Another Field
jsmi3279,xxxxxx,123456789,John,Smith,XMLT1005:MATH2987:PHYS4597:FRNC3578:ITLN590:LATN3490,,,,,,
glik2398,xxxxxx,123456789,Gill,Likely,XMLT1001:XMLT1004:MATH2987:PHYS4597,,,,,,


Still pulling my hair out, but thankyou for trying to help.
-J.
# 9  
Old 02-16-2007
Quote:
Originally Posted by reborg
Untested, but conceptually very similar to the previous post:

Code:
awk -F '[,:]' '{ for ( i=7, i <= NF i++) { printf "%s,%s,%s\n", $1, $2, $i } }

On Solaris you will need to use nawk instead of awk.

Sorry. No good.
Code:
awk -F '[,:]' '{ for ( i=7, i <= NF i++) { printf "%s,%s,%s\n", $1, $2, $i } }'

The error message is:
awk: syntax error at source line 1
context is
{ for ( >>> i=7, <<<


Any ideas?
-the newbie!
# 10  
Old 02-16-2007
Yes, it was as I said untested, and there was an obvious pair of typos in the for loop, still untested but should be ( I think ) correct now.

Code:
awk -F '[,:]' '{ for ( i=7; i <= NF; i++) { printf "%s,%s,%s\n", $1, $2, $i } }

# 11  
Old 02-16-2007
Quote:
Originally Posted by reborg
Yes, it was as I said untested, and there was an obvious pair of typos in the for loop, still untested but should be ( I think ) correct now.

Code:
awk -F '[,:]' '{ for ( i=7; i <= NF; i++) { printf "%s,%s,%s\n", $1, $2, $i } }


Nearly!
It was having problems with the six trailing ,,,,,, so I have stripped these out with sed first.

But it is still is skipping the first subject. I think this is because of the delimiter.
jsmi3279,xxxxxx,123456789,John,Smith,XMLT1005:MATH2987:PHYS4597:FRNC3578:ITLN590:LATN3490,,,,,,

The output has only 5 lines for this user not six. It doesn't contain a line with the first subject XMLT1005

Thanks for your assistance!
-J
# 12  
Old 02-16-2007
change the 7 to 6 in what I posted previously, I misread the input earlier.

Last edited by reborg; 02-17-2007 at 12:02 PM.. Reason: typo
# 13  
Old 02-16-2007
It works! Wow that's brilliant.
Thank you so much for the help and for rekindling esteem for the awesome awk.

The final version for anyone who wants to know:

awk -F '[,:]' '{ for ( i=6; i <= NF; i++) { printf "%s,%s,%s\n", $1, $2, $i } }'

Cheers!
-J.
# 14  
Old 02-17-2007
Quote:
Originally Posted by ynneycat
Hi Anbu
Thanks for this lovely looking awk array
awk -F"," ' {
str=$1
for( i = 2 ; i <= NF - 7 ; ++i )
str=str "," $i
n=split($(NF-6),arr,":")
for( i = 1 ; i <= n ; ++i )
print str "," arr[n]
} ' file[/INDENT]
Thanks but unfortunately this doesn't work & I'm too much of a newbie to fix it. It nearly works but rather than putting the next course on each line, it just repeats the same course. Output is:

User Name,Password,ID,First Name,Last Name,Subject
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
glik2398,xxxxxx,123456789,Gill,Likely,PHYS4597
glik2398,xxxxxx,123456789,Gill,Likely,PHYS4597

etc etc

What I want is output that looks like
User Name,Password,ID,First Name,Last Name,Subject
jsmi3279,xxxxxx,123456789,John,Smith,XMLT1005
jsmi3279,xxxxxx,123456789,John,Smith,MATH2987
jsmi3279,xxxxxx,123456789,John,Smith,PHYS4597
jsmi3279,xxxxxx,123456789,John,Smith,FRNC3578
jsmi3279,xxxxxx,123456789,John,Smith,ITLN590
jsmi3279,xxxxxx,123456789,John,Smith,LATN3490
glik2398,xxxxxx,123456789,Gill,Likely,PHYS4597
glik2398,xxxxxx,123456789,Gill,Likely,,XMLT1001
glik2398,xxxxxx,123456789,Gill,Likely,XMLT1004
glik2398,xxxxxx,123456789,Gill,Likely,MATH2987


from input that looks like this (the input in a file hundreds of lines long - this is just the first few lines)

User Name,Password,ID,First Name,Last Name,Subject,Other,#User Type,City,#Email,#Some Field,#Another Field
jsmi3279,xxxxxx,123456789,John,Smith,XMLT1005:MATH2987:PHYS4597:FRNC3578:ITLN590:LATN3490,,,,,,
glik2398,xxxxxx,123456789,Gill,Likely,XMLT1001:XMLT1004:MATH2987:PHYS4597,,,,,,


Still pulling my hair out, but thankyou for trying to help.
-J.
Code:
print str "," arr[i]

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Beginner: script headache

Hi, I am new to shell scripting, but doing my best to learn things. Today doing courses there was exercise like this: #!/usr/bin/bash a=$1 b=$2 if ] then echo "Conditions are met" else echo "Conditions are not met" fi I improved it a little, beacuse of curiosity :D to this: ... (8 Replies)
Discussion started by: Rosentine
8 Replies

2. Shell Programming and Scripting

Formatting file data to another file (control character related)

I have to write a program to read data from files and then format into another file. However, I face a strange problem related to control character that I can't understand and solve. The source file is compose of many lines with such format: T_NAME|P_NAME|P_CODE|DOCUMENT_PATH|REG_DATE ... (3 Replies)
Discussion started by: hk6279
3 Replies

3. UNIX for Advanced & Expert Users

ssh command headache

Good day, I am trying to run a ssh command on a remote server, but when I run the command the server takes FOREVER... and ends up giving me an error.. then the script also kicks server load up by 100% while it is running.. But if I run the same command on the server it goes pretty quickly and... (3 Replies)
Discussion started by: msullivan
3 Replies

4. Shell Programming and Scripting

Friday afternoon headache

Hi all, It's been a long week and my brain is clearly not functioning right so hopefully someone can help me out here. I've got a function in a script which just checks if a MySQL database directory exists or not. Code is as follows: dbCheck2() { if ; then { : # carry... (4 Replies)
Discussion started by: _Spare_Ribs_
4 Replies

5. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

6. UNIX for Dummies Questions & Answers

sed headache

Hi all, I've got a thorny sed/regex problem and I thought somebody out there might be able to help. I'd like to use sed in a Makefile to parse a path string. The path might look like this: case 1: /mnt/users/bob/build/src or case 2: /mnt/users/bob/build/src/foo or case 3: ... (4 Replies)
Discussion started by: gnarl
4 Replies

7. Shell Programming and Scripting

sed make me headache... need help!

I've still a little problem with sed. So, the content of my file.txt is like: 101.10.20.2.1079 > 101.11.2.20.80: 101.10.20.2.1080 > 101.11.2.20.80: 101.10.20.2.1081 > 101.10.20.2..80: 101.05.15.143.1068 > tpo68-96-22-1.no.no.cox.net.4391: 101.05.15.143.1072 >... (5 Replies)
Discussion started by: nymus7
5 Replies

8. Solaris

A very headache problem

guys, need your help~~~~ I am setting up a Sun Enterprise 450 Ultra 4 server originally I was using Sun solaris 9 1202 version. but after an accident (i guess becoz i deleted something wrongly), the system cannot boot up. and now I wana install a FreeBSD(since it is free and it will... (3 Replies)
Discussion started by: MrDJay
3 Replies

9. IP Networking

DHCP client & ipchains headache

Hi all, I upgraded my SUSE 6.1 to SUSE 7.1 and at once the following things won't work anymore: 1) My DHCP client is not able anymore to retrieve my IP address from the @home server. It times out all the time. If I use a fixed IP I can get on the net, so there is no physical problem. 2) I... (3 Replies)
Discussion started by: Micky
3 Replies

10. UNIX for Dummies Questions & Answers

extremely headache

I need to execute command at remote host from my program. there is rs6000/aix standard command called rexec. unfortunately rexec ask username and password which must supplied interactively, of course it is impossible runned by program. So I have to change rexec command format to make user-name... (5 Replies)
Discussion started by: yatno
5 Replies
Login or Register to Ask a Question