Formatting Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting Problem
# 1  
Old 03-15-2007
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  
Old 03-15-2007
Code:
awk -F" - " '{ if( $1 == prev ) { printf ",%s", $2 } else { printf "\n%s,%s", $1, $2; prev=$1; } }END {printf "\n"}' filename

# 3  
Old 03-15-2007
Code:
awk -F"-" ' 
{ 
   gsub(" +","",$0) ;
   arr[$1]=arr[$1] "," $2 
}
END { 
   for( key in arr ) { print key arr[key] } 
} ' file

# 4  
Old 03-15-2007
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 03:25 PM.. Reason: Output did not follow indents.
# 5  
Old 03-15-2007
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 01:07 AM..
# 6  
Old 03-16-2007
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 09:57 AM.. Reason: Remove original quoted message.
# 7  
Old 03-16-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in formatting output in sed / awk

I have a file like this : ! 1 ! 542255 ! 50,140.00 ! ! 2 ! 551717 ! 5,805.00 ! ! 3 ! 551763 ! 8,130.00 ! ! 4 ! 551779 ! 750.00 ! ! 5 ! 551810 ! 56,580.00 ! ! 6 ! 551816 ! 1,350.00 ! ! 7 ! 551876 ! 360.00 ! ! 8 ! 551898 ! ... (10 Replies)
Discussion started by: adam1969in
10 Replies

2. Shell Programming and Scripting

Output formatting problem

Hello; I have a simple loop filtering a log: for LU in $(< LU-list-Final) do OUT=$(grep -B1 $LU cibc-src-ip.cap |egrep 'IP 16|IP 19|IP 15' |awk -F">" '{print $1}') if ; then echo " LU $LU was accessed by ===============> $OUT " echo "" fi done The current output snippet looks like... (2 Replies)
Discussion started by: delphys
2 Replies

3. UNIX for Advanced & Expert Users

Dos2UNIX formatting problem

Hi, I was trying to dos2unix a file that has some special characters but dos2unix converted those into different format. I am working on sun server. I guess the default for dos2unix on sun server is ISO format . Can i change the format so that it does the conversion in UTF format? Because I... (3 Replies)
Discussion started by: abhi1988sri
3 Replies

4. Shell Programming and Scripting

trite formatting problem

Hello; having an annoying issue: I wish to have the same formatting in: awk '{print $1}' LOCAL f30f31be17a236378ac896639cc1b996 bff4c460f601444db6ef7f6ad6ca44b9 347a399b6fe9c2f21e6a7f55911c1483 ce3f8fdd4919e891090ca27872f4f983 c00098663f064d14065d0ef248a4db44... (2 Replies)
Discussion started by: delphys
2 Replies

5. Shell Programming and Scripting

Problem in formatting number

Hi, I was trying to format my number like i=1 to 000001 using the below method. typeset -Z6 i (sorry, corrected) My shell is K, is not doing, it is supposed to do Thanks in advance (6 Replies)
Discussion started by: ezee
6 Replies

6. Shell Programming and Scripting

Problem with formatting text with awk

I want the following output: User ID: 4071 Last Name: Gills First Name: Roberts Address: Maple Dr. Phone#: 702346789 from this command: grep "$uId" database.txt | awk -F":" '{print "User ID:\t"$uId"\nLast Name:\t"$lname"\n...etc. }' But all I get is this: User ID:... (3 Replies)
Discussion started by: yonkers062986
3 Replies

7. Shell Programming and Scripting

output formatting problem

I would like to keep the complete lines in the output, but my script adds carriage returns for each space (e.g. keep BRITISH AIRWAYS on one line in the output): File1= BAW BRITISH AIRWAYS RYR RYAN AIR for i in $(cat File1) do echo $i done Output: BAW BRITISH AIRWAYS RYR... (4 Replies)
Discussion started by: barny
4 Replies

8. Shell Programming and Scripting

AWK Formatting Problem

Hi All, I'm having a problem with the way awk is interperting a space between double quotes in a for loop. Below is the code and output from running the script: AWK for loop: for i in $(awk 'BEGIN{FS=","}{print "Probe Name:" $1};{print "Probe Temp:" $2};{ print... (2 Replies)
Discussion started by: cstovall
2 Replies

9. Shell Programming and Scripting

Problem in Formatting File

I am facing a very challenging task here but can't finish it.I request all of you to help me please. I have one file which contain some data i need to format it. data file contain data like 54321|item-68|owner|yes||||$ 00-10|invoice|3221|||# 00-11|invoice|3221|||#... (1 Reply)
Discussion started by: Dhruva
1 Replies

10. UNIX for Dummies Questions & Answers

Problem re-formatting Disk Partition

I have a disk formatted as follows. Part Flag Tag Cylinders Size =================================================== 0 wm root 0 - 38125 26.18 Gb 1 wu swap 38126 - 49776 8 Gb 2 wm backup 0 - 49779 34.18 Gb 3 wm unassigned 4 wm unassigned 5 wm unassigned 6 wm unassigned... (1 Reply)
Discussion started by: jimthompson
1 Replies
Login or Register to Ask a Question