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
AWK Formatting Problem cstovall Shell Programming and Scripting 2 11-30-2007 10:53 PM
rare problem -reinstalling formatting nothing is working simaich8 Linux 0 11-04-2007 02:07 AM
Formatting problem skyineyes Shell Programming and Scripting 9 07-03-2007 10:11 AM
Problem in Formatting File Dhruva Shell Programming and Scripting 1 04-11-2006 09:50 AM
Problem re-formatting Disk Partition jimthompson UNIX for Dummies Questions & Answers 1 01-11-2006 07:06 AM

Closed Thread
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 03-15-2007
PradeepRed PradeepRed is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 25
Formatting Problem

Hi
Suppose we have a file consisting of nos in following format

123 - 789
123 - 828
345 - 989
345 - 792

I require the following output

123, 789,828
345, 989,792
Means Unique nos in 1st Column and Corresponding two nos in comma separated 2nd Column
Please help me out...
  #2 (permalink)  
Old 03-15-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,953
Code:
awk -F" - " '{ if( $1 == prev ) { printf ",%s", $2 } else { printf "\n%s,%s", $1, $2; prev=$1; } }END {printf "\n"}' filename
  #3 (permalink)  
Old 03-15-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
awk -F"-" ' 
{ 
   gsub(" +","",$0) ;
   arr[$1]=arr[$1] "," $2 
}
END { 
   for( key in arr ) { print key arr[key] } 
} ' file
  #4 (permalink)  
Old 03-15-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Traditional solution.

## This is a traditional shell programming solution.
## It works, it can handle different cases,
## The only drawback is it has several lines of code.
##
## Run it as: cat <input_file> | <this_shell>
##
savefld1=""
Out=""
while read iLine
do
fld1=`echo $iLine | cut -f1 -d"-"`
fld2=`echo $iLine | cut -f2 -d"-"`
if [[ $fld1 = $savefld1 ]]; then
Out=$Out","$fld2
echo $Out
Out=""
else
if [[ $Out != "" ]]; then
echo $Out
Out=""
fi
Out=$fld1","$fld2
fi
savefld1=$fld1
done
if [[ $Out != "" ]]; then
echo $Out
fi

Last edited by Shell_Life; 03-15-2007 at 02:25 PM.. Reason: Output did not follow indents.
  #5 (permalink)  
Old 03-15-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Shell_Life
[FONT=Courier New]## This is a traditional shell programming solution.
This is not a traditional shell script; it is not even a POSIX shell script. It includes non-standard syntax, and doesn't take advantage of the features of the POSIX shell.

Quote:
## It works, it can handle different cases,
## The only drawback is it has several lines of code.
##
## Run it as: cat <input_file> | <this_shell>
Do not run it like that! There is no need for cat:

Code:
path_to_script < INPUTFILE
Quote:
##
savefld1=""
Out=""
while read iLine
do
fld1=`echo $iLine | cut -f1 -d"-"`
fld2=`echo $iLine | cut -f2 -d"-"`
In a POSIX shell, you do not need cut to split up a string:

Code:
fld1=${iLine%%-*}
fld2=${iLine#*-}
Quote:
if [[ $fld1 = $savefld1 ]]; then
That is non-standard syntax; use the standard [ ... ] instead:

Code:
if [ "$fld1" = "$savefld1" ]; then[/QUOTE]

Last edited by vgersh99; 03-19-2007 at 12:07 AM..
  #6 (permalink)  
Old 03-16-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Thank you for your information, cfajohnson.

I was just trying to help Pradeepred solve his problem.

We all know that there are innumerous ways to solve a problem in unix.

For the past 26 years, I have been working for several companies where,
the four most important things are:
1- To get the job done.
2- It does not break.
3- It is easy to understand.
4- It is easy to maintain it.

Again, I was just sincerely trying to help Pradeepred.

Sorry if I broke any rule writing any code that is not considered POSIX
shell script.

Best regards,
Marcos

Last edited by Shell_Life; 03-16-2007 at 08:57 AM.. Reason: Remove original quoted message.
  #7 (permalink)  
Old 03-16-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Shell_Life
Thank you for your information, cfajohnson.

I was just trying to help Pradeepred solve his problem.

We all know that there are innumerous ways to solve a problem in unix.

For the past 26 years, I have been working for several companies where,
the four most important things are:
1- To get the job done.
2- It does not break.
The point is that it WILL break if the original poster is not using the same shell as you.
Quote:
3- It is easy to understand.
4- It is easy to maintain it.

Again, I was just sincerely trying to help Pradeepred.

Sorry if I broke any rule writing any code that is not considered POSIX
shell script.
The reason for using a POSIX shell is to make the script portable. If you use a shell-specific, non-standard syntax, you may not be helping anyone.

Every *nix system has a POSIX shell, but it may not be bash or ksh93.
Closed Thread

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 09:44 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