The UNIX and Linux Forums  

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
Question about sort specific column and print other column at the same time ! patrick87 Shell Programming and Scripting 3 10-07-2009 07:43 AM
How to pick values from column based on key values by usin AWK repinementer Shell Programming and Scripting 16 07-24-2009 09:59 AM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 4 Weeks Ago
sncoupons sncoupons is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 12
How to sort values inside one column?

Hi,

Could someone please help me with this? I have a text file that has fields seperated by comma. The last column in it has multiple values seperated by "|". I need to sort values in the last column seperated by pipe..is there any way I can do this through script?

Sample text file -

Code:
TEST1,ABC,5|4|1|3|2
TEST2,DEF,10|7|25|11|52|19
TEST3,XYZ,5|11|52|1

The output should generate -

Code:
TEST1,ABC,1|2|3|4|5
TEST2,DEF,7|10|11|19|25|52
TEST3,XYZ,1|5|11|52


How can I achieve this?

---------- Post updated at 04:47 PM ---------- Previous update was at 03:46 PM ----------

Could anyone help please??
  #2 (permalink)  
Old 4 Weeks Ago
SFNYC SFNYC is offline
Registered User
  
 

Join Date: Jun 2008
Location: New York City
Posts: 95
Try this:


Code:
$ cat ./sort_fields.ksh
#!/bin/ksh

sort_array()
{
  set -A LOCALARRAY $1

  LOCALARRAY_COUNT=${#LOCALARRAY[*]}

  IDX=0
  TEMP=0
  while (( $IDX < $LOCALARRAY_COUNT ))
  do
    IDX2=`expr $IDX + 1`
    while (( $IDX2 < $LOCALARRAY_COUNT ))
      do
        if (( ${LOCALARRAY[$IDX]} > ${LOCALARRAY[$IDX2]} ))
        then
           TEMP=${LOCALARRAY[$IDX]}
           LOCALARRAY[$IDX]=${LOCALARRAY[$IDX2]}
           LOCALARRAY[$IDX2]=$TEMP
        fi
        IDX2=`expr $IDX2 + 1`
      done
    IDX=`expr $IDX + 1`
  done

 print "${LOCALARRAY[*]}"

}

while read LINE
do
 LINEPT1=${LINE%,*}
 set -A FIELDS $(echo $LINE | cut -d',' -f3 |cut -d'|'  --output-delimiter=' ' -f1-)
 LINEPT2=$(sort_array "${FIELDS[*]}"|cut -d' ' --output-delimiter='|' -f1-)
 print "${LINEPT1},${LINEPT2}"

done < test.dat

exit 0

$ cat ./test.dat
TEST1,ABC,5|4|1|3|2
TEST2,DEF,10|7|25|11|52|19
TEST3,XYZ,5|11|52|1

$ ./sort_fields.ksh
TEST1,ABC,1|2|3|4|5
TEST2,DEF,7|10|11|19|25|52
TEST3,XYZ,1|5|11|52

Bits Awarded / Charged to SFNYC for this Post
Date User Comment Amount
4 Weeks Ago Anonymous Awarding points to SFNYC just because he was the first one to help and it worked! Others also suggested great solutions. 2,000
  #3 (permalink)  
Old 4 Weeks Ago
mkastin mkastin is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 47
I still consider myself to definitely be a beginner with awk but your question intrigued me so I worked at it a bit. The following doesn't meet 100% of your requirement but I just can't figure it out from this point.


Code:
$ awk -F, '{split($3,a,"|"); n=asort(a,b); for (i=1; i<=n; i++) sort=sort "|" b[i]; print $1 "," $2 "," sort}' file

input

Code:
TEST1,ABC,5|4|1|3|2
TEST2,DEF,10|7|25|11|52|19
TEST3,XYZ,5|11|52|1

output

Code:
TEST1,ABC,|1|2|3|4|5
TEST2,DEF,|1|2|3|4|5|7|10|11|19|25|52
TEST3,XYZ,|1|2|3|4|5|7|10|11|19|25|52|1|5|11|52

As you can see the problem I'm having is getting the for loop to only include the parts of the array relevant to the current line.

I'm scratching my head for now, but I know I'll figure it out soon
  #4 (permalink)  
Old 3 Weeks Ago
rdcwayx rdcwayx is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 290
Quote:
Originally Posted by mkastin View Post

Code:
$ awk -F, '{split($3,a,"|"); n=asort(a,b); for (i=1; i<=n; i++) sort=sort "|" b[i]; print $1 "," $2 "," sort}' file

output

Code:
TEST1,ABC,|1|2|3|4|5
TEST2,DEF,|1|2|3|4|5|7|10|11|19|25|52
TEST3,XYZ,|1|2|3|4|5|7|10|11|19|25|52|1|5|11|52
I think I fix your code.


Code:
awk -F, '{sort=""} {split($3,a,"|"); n=asort(a,b); for (i=1; i<=n; i++) sort=sort "|" b[i]; print $1 "," $2 "," sort}' urfile
TEST1,ABC,|1|2|3|4|5
TEST2,DEF,|7|10|11|19|25|52
TEST3,XYZ,|1|5|11|52

Below is the code, which I try to make it shorter


Code:
$  awk -F, '{printf "\n"$1","$2","} {split($3,a,"|"); n=asort(a); for (i=1;i<=n;i++) printf "|"a[i]} ' urfile

TEST1,ABC,|1|2|3|4|5
TEST2,DEF,|7|10|11|19|25|52
TEST3,XYZ,|1|5|11|52


Last edited by rdcwayx; 3 Weeks Ago at 11:26 PM..
Bits Awarded / Charged to rdcwayx for this Post
Date User Comment Amount
3 Weeks Ago mkastin Thanks! 1,000
  #5 (permalink)  
Old 4 Weeks Ago
rdcwayx rdcwayx is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 290
Should be a simple solution, but stop to merge the lines. Anyone can help to continue it?


Code:
$ awk -F[\,\|] '{for(i=3;i<=NF;i++) {print $1,$2,$i}}' urfile |sort -k1,1n -k3,3n
TEST1 ABC 1
TEST1 ABC 2
TEST1 ABC 3
TEST1 ABC 4
TEST1 ABC 5
TEST2 DEF 7
TEST2 DEF 10
TEST2 DEF 11
TEST2 DEF 19
TEST2 DEF 25
TEST2 DEF 52
TEST3 XYZ 1
TEST3 XYZ 5
TEST3 XYZ 11
TEST3 XYZ 52

then I need merge the lines which $1 is same.
  #6 (permalink)  
Old 4 Weeks Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 761

Code:
sortbars() {
  echo "$1"|xargs -d'|' -n1|sort -n|xargs|tr ' ' '|'
}
while IFS=, read cola colb colc; do
  echo "$cola,$colb,$(sortbars $colc)"
done < infile


Code:
TEST1,ABC,1|2|3|4|5
TEST2,DEF,7|10|11|19|25|52
TEST3,XYZ,1|5|11|52

Alternatively:

Code:
sortbars() {
  IFS='|'; for i in $1; do
    echo $i
  done|sort -n|xargs|tr ' ' '|'
}

Bits Awarded / Charged to Scrutinizer for this Post
Date User Comment Amount
4 Weeks Ago Anonymous N/A 100
  #7 (permalink)  
Old 4 Weeks Ago
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099
perl:


Code:
while(<DATA>){
	chomp;
	my @tmp=split(",",$_);
	my @t1=split("[|]",$tmp[2]);
	$tmp[2]=join "|", sort {$a<=>$b} @t1;
	print join ",",@tmp;
	print "\n";
}
__DATA__
TEST1,ABC,5|4|1|3|2
TEST2,DEF,10|7|25|11|52|19
TEST3,XYZ,5|11|52|1

Reply

Bookmarks

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 06:01 AM.


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