convoluted code


 
Thread Tools Search this Thread
The Lounge War Stories convoluted code
# 1  
Old 07-04-2012
convoluted code

Hi,
I have been thinking of how to script this but i have no clue at all..
Could someone please help me out or give me some idea on this?
I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas.
Let's say i have the following input.
Code:
aa c1
aa c2
aa c3
cc d1
dd e1
dd e2
ee f1

I would like the output to be like this.
Code:
aa c1,c2,c3
cc d1
dd e1,e2
ee f1

Could this be easily done with bash script?
Or should i try perl script then?
I'm a beginner in bash script and perl.
Thank you.

***************************
Try this

Code:
first="Y"                
while read a b           
do                       
if [ "$first"  =  "Y"  ] 
then                     
   first="N"             
   prev=$a               
   echo "$a $b\c"        
else                     
if [ "$a" != "$prev" ]   
then                     
   echo "\n$a $b\c"      
   prev=$a               
else                     
   echo ",$b\c"          
fi                       
fi                       
done

This assumes that the input file is sorted.

********************************************************
Sorry.. don't understand..
what is a, b , F ..?
Anyway i found a short solution.
${input} is the filename for the input file.
Code:
for m in `cat ${input} | awk '{print $1}' | sort | uniq `
do
        var=`grep "^${m} " ${output} | awk '{print $2}' | tr '\n' ',' | sed '$s/,$//'`
        echo "${m} ${var}"
done

These 2 Users Gave Thanks to jgt For This Post:
# 2  
Old 07-04-2012
@jgt
Very good.

I love the way that the first script uses Bourne echo and never terminates the last line.

The "short solution" relies on the exponential multi-pass technique and utilises a magic Shell with unlimited command line length. Until looking at unix.com I had never seen the for var in <open-ended list> construct ever ... and still wonder which course/manual/book/rumour it comes from?


Footnote: For anybody following this thread, this is a discussion thread.
@jgt is expert and definitely did not ask this question.

Last edited by methyl; 07-04-2012 at 06:06 PM..
# 3  
Old 07-04-2012
Quote:
Originally Posted by methyl
Footnote: For anybody following this thread, this is a discussion thread.
@jgt is expert and definitely did not ask this question.
I must confess that I was confused until that footnote clued me in to the fact that I wasn't in the shell scripting forum anymore (got here via the 'new posts' link). Smilie


Quote:
Originally Posted by methyl
Until looking at unix.com I had never seen the for var in <open-ended list> construct ever ... and still wonder which course/manual/book/rumour it comes from?
I don't know for certain, but my first impulse is to blame GNU Bash. I've seen that idiom recommended as the correct way to work around the fact that the final command in a bash pipeline runs in a subshell.

From the Advanced Bash-Scripting Guide's Bash Gotchas:
Code:
# Loop piping troubles.
#  This example by Anthony Richardson,
#+ with addendum by Wilbert Berendsen.


foundone=false
find $HOME -type f -atime +30 -size 100k |
while true
do
   read f
   echo "$f is over 100KB and has not been accessed in over 30 days"
   echo "Consider moving the file to archives."
   foundone=true
   # ------------------------------------
     echo "Subshell level = $BASH_SUBSHELL"
   # Subshell level = 1
   # Yes, we're inside a subshell.
   # ------------------------------------
done
   
#  foundone will always be false here since it is
#+ set to true inside a subshell
if [ $foundone = false ]
then
   echo "No files need archiving."
fi

# =====================Now, here is the correct way:=================

foundone=false
for f in $(find $HOME -type f -atime +30 -size 100k)  # No pipe here.
do
   echo "$f is over 100KB and has not been accessed in over 30 days"
   echo "Consider moving the file to archives."
   foundone=true
done
   
if [ $foundone = false ]
then
   echo "No files need archiving."
fi

Regards,
Alister
# 4  
Old 07-04-2012
@jgt: Really funny... Smilie

Quote:
Originally Posted by methyl
@jgt
[..]The "short solution" relies on the exponential multi-pass technique and utilises a magic Shell with unlimited command line length. [..]
Although it may be good to note that command line length does not really play a role in this case, since "for" is a shell keyword and not an external command, so the length of the list should be more or less limited by memory...

Quote:
Originally Posted by alister
[..]I don't know for certain, but my first impulse is to blame GNU Bash. I've seen that idiom recommended as the correct way to work around the fact that the final command in a bash pipeline runs in a subshell[..]
It is not only bash, dash and Bourne shell do this too. It is also not specified by Posix, so ksh is the exception..

Last edited by Scrutinizer; 07-04-2012 at 06:42 PM..
# 5  
Old 07-04-2012
Quote:
Although it may be good to note that command line length does not really play a role in this case, since "for" is a shell keyword and not an external command, so the length of the list should be more or less limited by memory...
There have been so many posts from AIX users (to name but a few O/S versions and variants and for the benefit of @jlliagre that includes many versions of SunOS/Solaris!) who hit this problem that I would love that the for var in <open ended list> syntax would become a syntax error.

Somehow I knew that this thread was going to be fun.

Great posts @jgt, @alister and @Scrutinizer.
# 6  
Old 07-04-2012
Quote:
Originally Posted by Scrutinizer
It is not only bash, dash and Bourne shell do this too. It is also not specified by Posix, so ksh is the exception..
I agree with you. ksh is exceptional. Smilie

My post isn't intended to suggest that bash (or dash or bourne sh) violate the standard -- I'm aware that POSIX allows a shell to execute each component of a pipeline in either the current environment or a subshell (ksh is compliant in this respect); the post is merely a response to methyl's rumination on the popularity of the idiom, identifying one popular resource which promotes it.

In my opinion, the ksh approach makes life a little easier. But, if I depend on it in a script, I always explicitly require ksh in the shebang.

Regards,
Alister
# 7  
Old 07-05-2012
Quote:
Originally Posted by alister
My post isn't intended to suggest that bash (or dash or bourne sh) violate the standard -- I'm aware that POSIX allows a shell to execute each component of a pipeline in either the current environment or a subshell (ksh is compliant in this respect); the post is merely a response to methyl's rumination on the popularity of the idiom, identifying one popular resource which promotes it.
You weren't that wordy about it at the time; you simply said your first impulse was to "blame GNU bash". That's a bit of a sore point -- BASH, as a shell many new users start with, has a low value in some admin's eyes -- the habit of new users calling all shell scripting "bash scripts" doesn't help -- and seems to get blamed for everything from their own bugs to poor weather as a result. Even though it's just doing what a Bourne shell does. Blame him if you must. Smilie

Quote:
In my opinion, the ksh approach makes life a little easier.
Perhaps, but it's not something that can really be changed now. Bourne is what it is, and changing the ordering would change what happens in a lot of scripts because it cuts both ways -- suddenly scripts could be setting variables in the main shell they never did before...

I bet BASH'll add it as one of it's many fiddly setopt options someday.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Trying to use a convoluted for loop with VLC and Parallel or OpenMPI with no success. Help?

I have about 12,000,000 mod files I'm trying to turn into a test of "unlimited cloud storage" by running them all through VLC and blowing them into mp3 files. I can get this to work serially but when trying to use openMPI or Parallel, something in the syntax is tripping it up some. Here is an... (1 Reply)
Discussion started by: sparticus414
1 Replies

2. Linux

Go to a line of code , skip few lines of code

Hi , I have a code where i am using a infinite while loop . some thing like below while do if then #go to line 20 fi command 1; command 2; #line 20: sleep 34; (5 Replies)
Discussion started by: Paarth
5 Replies

3. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

4. UNIX for Dummies Questions & Answers

If ‘922’ Code does not exist on ‘03’ Record, ‘901’ Code will be there instead, move ‘03’ R

01,011600033,011600033,110516,0834,2,90,,2/ 02,011600033,011103093,1,110317,0834,,2/ 03,105581,,015,+00000416418,,,901,+00000000148,,,922,+000000 00354,,/ 03,113806,,015,+00000559618,,,901,+00000000096,,,922,+000000 00621,,/ 88,902,+0000000025218,,/... (1 Reply)
Discussion started by: sgoud
1 Replies

5. Shell Programming and Scripting

translate ksh code to csh code

hi all, Can any 1 help me translate this korn shell code to C shell code : email=$(grep "^$1" $folder/config_2.txt | awk '{print $2'}) In config_2.txt the content is : which mean in korn shell , $1=groupname and $2=email address. Now i need to write in C shell script,when i set the... (2 Replies)
Discussion started by: proghack
2 Replies

6. Programming

how i prepare a c++ code(c code) for implementing my own protocol format

helo my protocol format is given below { destno,mode,no.of packet,pktsize,,pktno,textsize,CRC} description:- { is starting flag destno - 4bytes mode - 1 byte no.of pkt - 4byes pktsize - 6 bytes ... (1 Reply)
Discussion started by: amitpansuria
1 Replies
Login or Register to Ask a Question