grabbing more than one argument in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grabbing more than one argument in a loop
# 1  
Old 02-19-2009
grabbing more than one argument in a loop

Hello all I hope someone can help me. I am trying to convert something I wrote in C to bash.

But how do I go about reading more than one item at a time in a for loop?
i have been using this format for the loops in the bash script i have been building.

e.g.
Code:
for word in `cat -s sortlist2.txt`
do
    echo -e "$word\n";
done

but now i need more than one variable per pass of the loop.

here is the format of the sortlist2.txt. it is the path/name and time-stamp space separated one entry per line:
  • p2_6f_9_19_3/Eci 1063673162
    p2_6f_9_19_3/Zargon 1063679315
    p2_6f_9_19_3/Weslyn 1063681624
    p2_6j_10_8_4/Weslyn 1063681624
    p2_6k_12_11_4/Weslyn 1063681624
    p2_6l_2_16_5/Weslyn 1063681624
    p2_6f_9_19_3/Eilonwy 1063686817
    p2_6f_9_19_3/Aloof 1063686900
    p2_6f_9_19_3/Papertiger 1063688780
    p2_6f_9_19_3/Bytor 1063689879
    p2_6f_9_19_3/Ziggs 1063709714
    p2_6f_9_19_3/Tyrael_rcls 1063711697
    p2_6f_9_19_3/Cypress 1063716672
    p2_6f_9_19_3/Neuromancer 1063717200
    p2_6f_9_19_3/Endram 1063730040
    p2_6j_10_8_4/Endram 1063730040
    p2_6k_12_11_4/Endram 1063730040
    p2_6l_2_16_5/Endram 1063730040
    p2_6f_9_19_3/Bixx 1063736835
    $
The C code reads in the data and keeps the first unique entry (saved to sortlist3.txt) and puts the repeat time-stamp entries in a file (rmlist.txt) for removing those player files later.

here is the C code:
Code:
      if ((fpr = fopen("../../../parchive/rmlist.txt", "w")) == NULL)
      {
            perror("couldn't open rmlist.txt for writing.");
            return;
      }

       if ((fp = fopen("../../../parchive/sortlist2.txt", "r")) != NULL)
       {
            name = fread_word(fp);
            logo = fread_number(fp);

            if ((fpo = fopen("../../../parchive/sortlist3.txt", "w")) != NULL)
            {
                fprintf(fpo, "%s %ld\n", name, logo);                    

                for (;;)
                {
                    prev_name = name;
                    prev_logo = logo;

                    name = fread_word(fp);

                    if (name[0] == '$')
                    {
                        fprintf(fpo, "$\n");
                        break;
                    }

                    logo = fread_number(fp);

                    if (logo == prev_logo)
                    {
                        fprintf(fpr, "%s\n", name);                    
                        continue;
                    }

                    fprintf(fpo, "%s %ld\n", name, logo);                    
                }
            }
            else
            {
                perror("couldn't open sortlist3.txt for writing.");
            }
            
            fclose(fp);
            fclose(fpo);
            fclose(fpr);
        }

or is there some slick bash command(s) that can do this sorting?
I can get rid of the $ at the end of the file if needed. I just needed it for C to detect the end of the list. I thought about reading the file into two arrays, $name[] and $logo[] and then do the filtering. Possibly use the read command and a while statement? Thanks.
# 2  
Old 02-19-2009
I think this will do what you want (or close enough that you can take it from here):
Code:
cut -d" " -f1 sortlist2.txt| uniq > sortlist3.txt
cut -d" " -f2 sortlist2.txt| sort| uniq > rmlist.txt

# 3  
Old 02-19-2009
CPU & Memory

Hi Intense

Could you please share you required output sample data with input file...
so that makes clear picture...

Thanks
Sha
# 4  
Old 02-19-2009
Quote:
Originally Posted by Intense4200
But how do I go about reading more than one item at a time in a for loop?
First off, you shouldn't use for-loops for that purpose at all. A for-loop loops through a list of values and as soon as this list gets longer than the maximum line length the shell can handle (as per POSIX 4096 characters are guaranteed) the command will break.

The shell evaluates first what you have written in backticks and replaces the command with its output, basically putting the whole content of the file on the command line. Only then the for-loop is executed.

Use a while-loop for that purpose so you do not run the risk of the commandline getting too long with the added benefit of being able to cycle any number of variables instead of only one:

Code:
cat /path/to/file | while read valueA valueB ; do
     echo Value1 = ${valueA} \tValue2 = ${valueB}
done

Some might say that it would be possible to save the one pipeline by writing the feeding file at the end of the loop:

Code:
while read valueA valueB ; do
     echo Value1 = ${valueA} \tValue2 = ${valueB}
done < /path/to/file

While this is syntactically correct IMHO the first version is easier to read and to maintain, which is why i prefer to do it this way.

I hope this helps.

bakunin
# 5  
Old 02-20-2009
Quote:
Originally Posted by Shahul
Hi Intense

Could you please share you required output sample data with input file...
so that makes clear picture...

Thanks
Sha
ok thanks to everyone so far; bakunin for the loop/read help, and hard aix; using your examples made me realize I have an error in my C code.
a contingency I did not realize was a possibility, filtering stuff that should not be filtered. the orig sortlist2.txt file has 14655 entries to sort through so it is a long file.

Here is a different section from the sortlist2.txt file:
  • p2_6l_2_16_5/Anteros 1091470965
    p2_6j_10_8_4/Erekiteru 1091550399
    p2_6j_10_8_4/Sabre 1091566980
    p2_6k_12_11_4/Sabre 1091566980
    p2_6l_2_16_5/Sabre 1091566980
    p2_6j_10_8_4/Lucy 1091573700
    p2_6j_10_8_4/Ads 1091578595
    p2_6k_12_11_4/Ads 1091578595
    p2_6l_2_16_5/Ads 1091578595
    p2_6j_10_8_4/Alral 1091578662
    p2_6k_12_11_4/Alral 1091578662
    p2_6l_2_16_5/Alral 1091578662
    p2_6j_10_8_4/Galena 1091598651
    p2_6k_12_11_4/Galena 1091598651
    p2_6k_12_11_4/Zippy 1091648261 <----- my code and 'cut' doesn't
    p2_6l_2_16_5/Zippy 1091648261 <-----
    p2_6j_10_8_4/Mijual 1091648261 <----- take into account this situation
    p2_6j_10_8_4/Kaya 1091696023
    p2_6k_12_11_4/Kaya 1091696023
    p2_6l_2_16_5/Kaya 1091696023
    p2_6j_10_8_4/Sej 1091721803
    p2_6j_10_8_4/Dewsfanclub 1091728387


Here is an example how the filter should work:
  • p2_6l_2_16_5/Anteros 1091470965
    p2_6j_10_8_4/Erekiteru 1091550399
    p2_6j_10_8_4/Sabre 1091566980
    p2_6j_10_8_4/Lucy 1091573700
    p2_6j_10_8_4/Ads 1091578595
    p2_6j_10_8_4/Alral 1091578662
    p2_6j_10_8_4/Galena 1091598651
    p2_6k_12_11_4/Zippy 1091648261
    p2_6j_10_8_4/Mijual 1091648261
    p2_6j_10_8_4/Kaya 1091696023
    p2_6j_10_8_4/Sej 1091721803
    p2_6j_10_8_4/Dewsfanclub 1091728387


here is how the sortlist3.txt should look:
  • p2_6l_2_16_5/Anteros
    p2_6j_10_8_4/Erekiteru
    p2_6j_10_8_4/Sabre
    p2_6j_10_8_4/Lucy
    p2_6j_10_8_4/Ads
    p2_6j_10_8_4/Alral
    p2_6j_10_8_4/Galena
    p2_6k_12_11_4/Zippy
    p2_6j_10_8_4/Mijual
    p2_6j_10_8_4/Kaya
    p2_6j_10_8_4/Sej
    p2_6j_10_8_4/Dewsfanclub


and the rmlist.txt would have the removed entries to be used later with the rm command:

  • p2_6k_12_11_4/Sabre
    p2_6l_2_16_5/Sabre
    p2_6k_12_11_4/Ads
    p2_6l_2_16_5/Ads
    p2_6k_12_11_4/Alral
    p2_6l_2_16_5/Alral
    p2_6k_12_11_4/Galena
    p2_6l_2_16_5/Zippy
    p2_6k_12_11_4/Kaya
    p2_6l_2_16_5/Kaya


So from a C standpoint i would need to, if the previous logout is the same as the one currently being read, then isolate the name from the directory and compare names also.

so from bash i would need to replace the / with a space to create 3 arguments per line, do the comparisons. but keep the / in the outputted lists.

and finally here is how the sortlis2.txt is being generated:
Code:
#!/bin/bash

rm blahlist.txt;
for adirfile in `cat -s adir.lst`
do
   echo -e "\e[0m adding list from \e[31m $adirfile \e[36m\n\n";
   grep  -m 1 LogO $adirfile/* >> blahlist.txt;
done

   echo -e "sorting list...\n\r";
   sort -g -k 2 blahlist.txt > sortlist.txt;

   echo -e "removing grep data...\n\r";
   sed  's/:LogO//' sortlist.txt > sortlist2.txt;
   echo "$" >> sortlist2.txt;
   echo -e "\e[0m";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Change argument in a for loop

In a "for i in *FD.CPY do" loop, I need to change .CPY to .layout so the executed command would be reclay blahFD.CPY >blahFD.layout What do I need to do to modify a copy of i to use after the > symbol? TIA (5 Replies)
Discussion started by: wbport
5 Replies

2. Shell Programming and Scripting

Grabbing fields with perl

I have several .csv files containing data like this: field_1;field_2;date;comment;amount; I want to extract the 3 last fields and load them in a database. my input_file = "/dir/file.csv"; my output_file = "/dir/file.sql"; open my $csv_file, '<', $input_file or die "Can't... (1 Reply)
Discussion started by: freddie50
1 Replies

3. Shell Programming and Scripting

Expect Scripting Loop Argument Desperately Needed!

I am trying to create an Expect script that does the following: 1) Telnets to an IP address and logs in with user ID and Password 2) Issue a CLI command to the server that will output data of which I am particularly interested in a DS1 clock 'Slips' value. I want to be able to keep issuing... (0 Replies)
Discussion started by: dwightlaidler
0 Replies

4. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

Grabbing Certain Fields

ok, so a script i wrote spits out an output like the below: 2,JABABA,BV=114,CV=1,DF=-113,PCT=99.1228% as you can see, each field is separated by a comma. now, how can I get rid of the first field and ONLY show the rest of the fields. meaning, i want to get rid of the "2,", and... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. UNIX for Dummies Questions & Answers

Grabbing a value from an output file

I am executing a stored proc and sending the results in a log file. I then want to grab one result from the output parameters (bolded below, 2) so that I can store it in a variable which will then be called in another script. There are more details that get printed in the beginning of the log file,... (3 Replies)
Discussion started by: hern14
3 Replies

9. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

10. Shell Programming and Scripting

Argument passing using for or while loop

Hi All, My query is as below: Am basically writing a parser script. My input file has got some variables which are populated by the calling program. callig program: fun1("cat","dog","cow") input.* argument first argument second I want to write a script that should give me... (4 Replies)
Discussion started by: jisha
4 Replies
Login or Register to Ask a Question