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.
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:
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:
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.
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:
Some might say that it would be possible to save the one pipeline by writing the feeding file at the end of the loop:
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.
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:
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)