Help formatting a string. Something like printf?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help formatting a string. Something like printf?
# 1  
Old 02-22-2010
Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file:

ID X
1 7
1 8
1 3
2 5
2 7
2 2

To something like this:

ID X1 X2 X3
1 7 8 3
2 5 7 2

I've tried the following loop:
for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i tstb.csv | awk -F "," '{i=2;for(j=1;j<NR;j++) printf "%s,",$i;j=NR;printf "%s,",$i"\n"}';done>out.csv

But I'm getting the following
X
,7
,8,8
,3,3,3
,5
,7,7
,2,2,2

So the loop is stuffed and it looks like I'm having a problem with newlines as well. Can anyone offer a solution to help?
# 2  
Old 02-22-2010
One way to do it with Perl:

Code:
$ 
$ cat f7
ID X
1 7
1 8
1 3
2 5
2 7
2 2
$ 
$ 
$ ##
$ perl -lane 'BEGIN {print "ID X1 X2 X3 X4"}
>             if (!/^ID/) { chomp;
>               if ($p eq "") {printf $_; $p=$F[0]}
>               elsif ($F[0]==$p) {printf " $F[1]"}
>               else {printf "\n$_"; $p=$F[0]}
>             } END {print}' f7
ID X1 X2 X3 X4
1 7 8 3
2 5 7 2
$ 
$

tyler_durden
# 3  
Old 02-22-2010
Assuming they always come in sets of 3:

Code:
echo ID X1 X2 X3
sed 1d data | paste -d' ' - - - | cut -d' ' -f1-2,4,6

... where "data" is the name of the file to be processed, whose first line is a discarded header ("ID X").

Regards,
Alister

Last edited by alister; 02-22-2010 at 11:21 PM..
# 4  
Old 02-22-2010
If you dont want the output sorted, try:

Code:
awk 'BEGIN { print "ID X1 X2 X3"; }NR>1{ A[$1]=A[$1]" "$2; } END { for (i in A) { print i A[i]; } }' file

# 5  
Old 02-23-2010
Thanks for the suggestions. I ended up using something similar to the first suggestion, but written in python. I was hoping to do it in a bash script, but had some trouble with the other two suggestions. Thanks again for your help. If there is any interest I can put up my python script.
Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printf padded string

Is possible to print padded string in printf? Example echo 1 | awk '{printf("%03d\n", $1)}' 001I want S1 S11 S2 S21to be padded as: S01 S11 S02 S21Thanks! (26 Replies)
Discussion started by: yifangt
26 Replies

2. Programming

Passing printf formatting parameters as variables

Hi, I haven't programed in C in a few years. I have been doing a lot of shell scripting, I.E. not really programming anything heavy. :o That said, I have a script that gives hourly usage statistics for our email server. It runs w-a-y to slow as a script for my impatience, and needs to... (7 Replies)
Discussion started by: mph
7 Replies

3. Shell Programming and Scripting

Unable to match string within awk printf

Hi All I am working to process txt file into csv commo separated. Input.txt 1,2,asdf,34sdsd,120,haahha2 2,2,wewedf,45sdsd,130,haahha ..... .... Errorcode.txt 120 130 140 myawk.awk code: { BEGIN{ HEADER="f1,f2,f3,f4,f5,f6" (4 Replies)
Discussion started by: krsnadasa
4 Replies

4. Shell Programming and Scripting

Text Formatting using printf[ksh]

Hi All, I am attempting to create a fixed length tilde delimited file using printf. The variables are initialized to fixed length blank spaces a=' ' b=' ' c=' ' d=' ' Sometimes the variable might contain values and sometimes they are... (5 Replies)
Discussion started by: angie1234
5 Replies

5. Shell Programming and Scripting

How to print a string using printf?

I want to print a string say "str1 str2 str3 str4" using printf. If I try printing it using printf it is printing as follows. output ------- str1 str2 str3 str4 btw I'm working in AIX. This is my first post in this forum :) regards, rakesh (4 Replies)
Discussion started by: enigmatrix
4 Replies

6. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

7. UNIX for Dummies Questions & Answers

printf formatting

Is there a way to make these 2 numbers - $482477.37 and $1875000.00 look like $482,477.37 and $1,875,000.00 with printf? (4 Replies)
Discussion started by: nickg
4 Replies

8. Shell Programming and Scripting

Explanation for printf string in awk

hi all can any one help me to understand this bdf -t vfxs | awk '/\//{printf("%-30s%-10s%-10s%-10s%-5s%-10s\n",$1,$2,$3,$4,$5,$6)}' i want to understand the numbers %-30S% (4 Replies)
Discussion started by: maxim42
4 Replies

9. Shell Programming and Scripting

printf with Character String

I am trying to use printf with a character string that is used within a do loop. The problem is that while in the loop, the printf prints the variable name instead of the value. The do loop calls the variable name from a text file (called device.txt): while read device do cat $device.clean... (2 Replies)
Discussion started by: dleblanc67
2 Replies

10. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies
Login or Register to Ask a Question