Shell scripting task


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell scripting task
# 1  
Old 04-25-2011
Shell scripting task

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Hi all,
I am new to shell scripting. And I have a task to do, I tried all possible ways to solve this, but didn't. Iwas wondering maybe someone could help me.

So I have a random file something like this

Andrew John Mike
Alfa Omega Beta

And I need to create scrip witch would filter any file like this:
- Find two most repeated symbols in file ( in this file I think it would be "a")
- Find most repeated line end in this file
- And symbol combination, witch is not in combination of symbols witch i entered
like this "joh"
And write all these to new file
I am really appreciate if someone would help me.
Thank You.


2. Relevant commands, code, scripts, algorithms:
awk, grep

3. The attempts at a solution (include all code and scripts):
I tried this:
Code:
#!/bin/bash
mas=( Andrew John Mike )
for i in ${#mas[@]}
do
  echo "Emelents count ${#mas[@]}"
  echo ${mas[0]}
done

Well i tried many diffrent ways, I don't know solution for first task, so I can't go any further, cause other tasks are familiar. I just can't go on. Help please .

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Kaunas College, Kaunas, Lithuania, first course, proffesor R.Kraunevičienė

Last edited by Scott; 04-25-2011 at 03:52 PM.. Reason: Added code tags
# 2  
Old 04-25-2011
${#mas[@]} This returns one single number, so the for-loop will loop once on i=3 then break. I think you meant for STR in "${mas[@]}" to loop over all elements in the array.

If you have to read from a file, start there:

Code:
while read -A ARR
do
        echo "${#ARR[@]} elements"
        for STR in "${ARR[@]}"
        do
                ...
        done
done < filename

Then you could just print each STR char by char:

for ((N=0; N<${#STR}; N++)) ; do echo "${STR:$N:1}" ; done

Then pipe the entire outer 'while read' loop through sort, which will make a file like
Code:
a
a
a
b
b
b
...

which you can then pipe into uniq -c to count them for you:
Code:
18 e
6 a
8 z
...

...then sort | tail -n 2 to get the bottom two.

Not the most efficient solution perhaps, but counting chars is a bit awkward to do in shell.
# 3  
Old 04-25-2011
Ok, I am sorry maybe I am really stupid. I try to do this:
Code:
strongid@ktl ~ $ pico sav1

   UW PICO(tm) 4.10                             File: sav1                                 Modified

#!/bin/bash
while read -A band (that's my file)
do
  echo "${#band[@]} elements"
  for STR in "${band[@]}
  do
  done
done
done < savas
                                          [ Wrote 9 lines ]

strongid@ktl ~ $ cat band (not empty)
jonas algis andrius
tomas rimas karolis
saulius justas domas

strongid@ktl ~ $ sh sav1 (try to do this script)
sav1: 5: Syntax error: Bad substitution

So with this error I cant go further to do read file and try to count symbols Smilie

Last edited by Scott; 04-25-2011 at 03:56 PM.. Reason: Code tags
# 4  
Old 04-25-2011
You're redirecting the file into the entire while loop, not just into 'read'. That <filename at the bottom is where you want to put the file. All read -A band does is tell read to read stdin into the array 'band'.

You've got one too many done's.

If you copy-paste the loop I have it will work(change ... into true), then you can add onto it.
# 5  
Old 04-25-2011
I copy all You written, then:

Code:
strongid@ktl ~ $ pico sav1
  
 UW PICO(tm) 4.10                             File: sav1

#!/bin/bash
while read -A ARR
do
    echo "${#ARR[@]} elements"
    for STR in "${ARR[@]}"
    do
    true
    done
done < band

                                           [ Read 9 lines ]

strongid@ktl ~ $ sh sav1
sav1: 5: Syntax error: Bad substitution

I don't exactly understand what ARR means, is it my array from band file?
so it's something like this [jonas algis andrius] ?
[tomas rimas karolis]
[saulius justas domas]
# 6  
Old 04-25-2011
Quote:
sav1: 5: Syntax error: Bad substitution
Well, what is line 5? I can't tell from what you pasted where the file begins or ends. #!/bin/bash has to be the very first line or you might not get the bash shell. Also try running it with 'bash filename.sh'

Quote:
I don't exactly understand what ARR means, is it my array from band file?
ARR is an array. On the first loop, ARR[0] will be jonas, ARR[1] will be algis, ARR[2] will be andrius.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-26-2011
So I done everything You told. Sorry to bother You with my low skills with shell.
Now I have this code
Code:
#!/bin/bash
while read -a ARR
do
    echo "${#ARR[@]} elements"
    for STR in "${ARR[@]}"
    do
    true
    done
done < band
for ((N=0; N<${#STR}; N++)); do echo "${STR:$N:1}"; done

Then I bash it:
Code:
strongid@ktl ~ $ bash sav1
3 elements
3 elements
3 elements
d
o
m
a
s

So I understand it, it finds 3 elements in every line like 3 words in every line, that's ok, but it takes only last one of these "domas" not all of them in one line and then I can't sort this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

2. Shell Programming and Scripting

Whether Shell script can do this task ???

In following attached 748phy.xls file, fifth column is ST_Date, which contains time and dates in this format 22-11-2012 7:54:54 PM in single column I want it to split in this format either 1st column 22/11/2012 and in second column 7:54:54 PM Or like this in separate... (13 Replies)
Discussion started by: Akshay Hegde
13 Replies

3. Shell Programming and Scripting

Can any programmer do this task in shell script...

input file's one set header is this, ----------------------------------------------------- OUTPUT FROM ASCII FILE: CAST #1 ----------------------------------------------------- CC Cruise Latitude Longitude YYYY MM DD Time Cast #Levels CA 8504 50.083 -144.883 1970 1 2... (6 Replies)
Discussion started by: Akshay Hegde
6 Replies

4. Shell Programming and Scripting

Shell scripting task

Hi all, I am new to shell scripting. And I have a task to do, I tried all possible ways to solve this, but didn't. Iwas wondering maybe someone could help me. So I have a random file something like this Andrew John Mike Alfa Omega Beta And I need to create scrip witch would filter any... (1 Reply)
Discussion started by: Strongid
1 Replies

5. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as <TASKLIST <TASK definition="Completion date" id="Taskname1" Some other <CODE name="Code12" <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter> <Parameter... (3 Replies)
Discussion started by: MissI
3 Replies

8. Shell Programming and Scripting

comment and Uncomment single task out of multiple task

I have a file contains TASK gsnmpproxy { CommandLine = $SMCHOME/bin/gsnmpProxy.exe } TASK gsnmpdbgui { CommandLine = $SMCHOME/bin/gsnmpdbgui.exe I would like to comment and than uncomment specific task eg TASK gsnmpproxy Pls suggest how to do in shell script (9 Replies)
Discussion started by: madhusmita
9 Replies

9. Shell Programming and Scripting

Optimized way of doing the task in shell programming

Hi I have a file consists of the following similar lines (10 mb file) 2008-05-15 02:15:38,268 RMSConnectionFactory - Setting session state for connection. 2008-05-15 02:15:38,277 RMSConnectionFactory - Returning WS connection. My task is to find out any missing second lines for... (14 Replies)
Discussion started by: pcjandyala
14 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question