Need help writing array for the specific need of shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help writing array for the specific need of shell script
# 1  
Old 07-16-2014
Code Need help writing array for the specific need of shell script

I need shell script for the following specfic need, I'll get following output after running my program.
I wanted every 50th row in the coloumn and take into array.

For example input
Code:
==============
03
03
03
03
05
05
05
05
07
07
07
07

I wanted to extract 3,5,7 and store it in array. extract the array if i want to use the array again.

out ---->
Code:
3,5,7

please suggest a way to cut the specified row and store it in array.

Last edited by JITHENDER; 07-17-2014 at 05:14 AM.. Reason: Add CODE tags.
# 2  
Old 07-16-2014
Is this a homework assignment?

I'm completely confused about what you are trying to do. You have shown us a file that contains 12 or 13 lines (I had to guess when I added CODE tags for you whether or not the line containing the equal signs is part of your input file). Then you say that you want every 50th row (of which there are none). And then you say you want 3, 5, and 7 (none of which appear in your input file). And you say you want those values stored in an array. What kind of array (shell, awk, ???)?

What OS are you using?

What shell are you using?

What language are you using?

What have you tried?
# 3  
Old 07-16-2014
Sorry for not mention clearly. I say it is example which i wrote here.
I need shell script to extract 3,5,7 which are in every 4th row for the below example.
==============
Code:
03
03
03
03
05
05
05
05
07
07
07
07

I am using Linux bash shell.. I don't know array concept in shell scripting i just know basics of shell programming.
I tried using
Code:
cut -f 1,4

and
Code:
cut -f 1,8

but it doesn't get the exact location.

---------- Post updated at 02:36 PM ---------- Previous update was at 02:06 PM ----------

Quote:
Originally Posted by Don Cragun
Is this a homework assignment?

I'm completely confused about what you are trying to do. You have shown us a file that contains 12 or 13 lines (I had to guess when I added CODE tags for you whether or not the line containing the equal signs is part of your input file). Then you say that you want every 50th row (of which there are none). And then you say you want 3, 5, and 7 (none of which appear in your input file). And you say you want those values stored in an array. What kind of array (shell, awk, ???)?

What OS are you using?

What shell are you using?

What language are you using?

What have you tried?
Sorry for not mention clearly. I say it is example which i wrote here.
I need shell script to extract 3,5,7 which are in every 4th row for the below example.
==============
Code:
03
03
03
03
05
05
05
05
07
07
07
07

I am using Linux bash shell.. I don't know array concept in shell scripting i just know basics of shell programming.
I tried using
Code:
cut -f 1,4

and
Code:
cut -f 1,8

but it doesn't get the exact location and also i wanted to store it in array after extracting it.

Last edited by JITHENDER; 07-17-2014 at 05:16 AM.. Reason: added code tags
# 4  
Old 07-16-2014
Moderator's Comments:
Mod Comment Please use CODE tags!


Show us clearly (with CODE tags) what is actually in your input file.

Show us exactly (with CODE tags) what you are trying to extract from that input file.

How would using cut to extract the first and 4th (or first and eighth) tab delimited fields from lines that contain only one field (and no tabs) help you extract every fourth line from a file.

If you don't understand the concept of an array in shell scripting, why do you want an array in your shell script? What are you going to do with that array?

I repeat 3, 5, and 7 do not appear in any row in your file. Your file contains 03, 05, and 07, respectively. What transformation are you trying to perform? Are you trying to convert octal input to decimal values? Are you trying to strip leading zeros from leading zero-filled decimal values?

I repeat: IS THIS A HOMEWORK ASSIGNMENT?
# 5  
Old 07-16-2014
Quote:
Originally Posted by Don Cragun
Moderator's Comments:
Mod Comment Please use CODE tags!


Show us clearly (with CODE tags) what is actually in your input file.

Show us exactly (with CODE tags) what you are trying to extract from that input file.

How would using cut to extract the first and 4th (or first and eighth) tab delimited fields from lines that contain only one field (and no tabs) help you extract every fourth line from a file.

If you don't understand the concept of an array in shell scripting, why do you want an array in your shell script? What are you going to do with that array?

I repeat 3, 5, and 7 do not appear in any row in your file. Your file contains 03, 05, and 07, respectively. What transformation are you trying to perform? Are you trying to convert octal input to decimal values? Are you trying to strip leading zeros from leading zero-filled decimal values?

I repeat: IS THIS A HOMEWORK ASSIGNMENT?
No this is not home work assignment. I had some requirement to restart some of the killed process. my program generate some files(like /tmp/grepd.0003.00049:12:server_read_cb:xxld died) when some process died. So based on the input from the file i have to extract these numbers and manipulate to restart those processes.
Code:
grep -rn "died" /tmp/grepd.0*|cut -d':' -f1|cut -d'.' -f2|cut -c 3,4

out File contains following after executing above code
=======
Code:
03
03
03
03
05
05
05
05
07
07
07
07

======

so from above out file, i want to extract 03,05,04,07 which is on every fourth row and convert to decimal value, store it in an array.

Moderator's Comments:
Mod Comment Please use CODE tags for sample input, sample output, and code as directed in private mail you received shortly after you posted the 1st message in this thread.

Last edited by Don Cragun; 07-16-2014 at 04:25 PM.. Reason: Change Bold tags to ICODE tags, add missing CODE tags.
# 6  
Old 07-16-2014
defining array is like below

set -A array_name value1 value2 ... valuen

get the uniq contents with
Code:
sort -u

and put a loop over the uniq contents and write into array. job done!!
# 7  
Old 07-16-2014
How about (assuming you are using bash which you failed to mention)
Code:
ARR=($(awk '/died/ {T[$2]} END {for (i in T) print i+0}' FS="." file))

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help writing shell script!

Hi, I'm very new to this, so bear with me please. I want to write a sh script (or if there's a better format please let me know) that allows me to, when I run it, print the date to a file (1.out) take 2 arguments (files a.fa and b.fa), run them with another program, outputting to 2.out, and then... (4 Replies)
Discussion started by: ShiGua
4 Replies

2. Shell Programming and Scripting

Need help writing shell script!

Hi, I'm very new to this, so bear with me please. I want to write a sh script (or if there's a better format please let me know) that allows me to, when I run it, print the date to a file (1.out) take 2 arguments (files a.fa and b.fa), run them with another program, outputting to 2.out, and then... (2 Replies)
Discussion started by: ShiGua
2 Replies

3. Shell Programming and Scripting

Need help in writing a script to create a new text file with specific data from existing two files

Hi, I have two text files. Need to create a third text file extracting specific data from first two existing files.. Text File 1: Format contains: SQL*Loader: Release 10.2.0.1.0 - Production on Wed Aug 4 21:06:34 2010 some text ............so on...and somwhere text like: Record 1:... (1 Reply)
Discussion started by: shashi143ibm
1 Replies

4. Shell Programming and Scripting

help writing rm script excluding specific titled dir

I am attempting to write a housecleaning script that does the following: 1) goes to a specific directory 2) deletes all contents of that directory but a specific directory within it. So my users all keep and use the Shared directory in OSX. Within /Users/Shared there are also standard named... (1 Reply)
Discussion started by: nomados
1 Replies

5. Shell Programming and Scripting

Need help in writing the shell script

Can anyone please help me in writing a shell script that would check if a particular user(xyz) has logged in, and if yes, the audit daemon needs to be started. When the user logs off the dameon needs to shutdown , and the report needs to be e-mailed to a set of users. (12 Replies)
Discussion started by: ggayathri
12 Replies

6. UNIX for Dummies Questions & Answers

can anybody help me out in writing the script for incrementing the specific field

can anybody help me out in writing the any script (awk,shell or perl script) for incrementing the specific field highlighted below /{/data/{/assetMetricsList//{1]/dailyCount,........./{/data/{/assetMetricsList//{100]/dailyCount It should be in below format in oneline seperated by commas ... (1 Reply)
Discussion started by: swapnak
1 Replies

7. Shell Programming and Scripting

Writing shell script

Hi, I am a new for shell script. i need to write script using the following commands cd /usres/test # create directory mkdir temp+DATE( i need to append date ) #moving files from one directory to this directory(we need to check total files in source and taget) cd /users/sample ... (2 Replies)
Discussion started by: bmkreddy
2 Replies

8. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

9. Shell Programming and Scripting

Need help for writing shell script

Hello ALL, I am fresher in Unix . i need help to write small shell script . Please help me unix guru. I am developing the internal site in my office . the data files are generated in one directory everyday . I have to write shell script to sort those files and put it is internal site . ... (3 Replies)
Discussion started by: deepa20
3 Replies

10. Shell Programming and Scripting

Need help with writing shell script

I have the following output. I want to write a script to check for 1. waits > 0 on all rowsand Ratio > .0. if true then send email. ========================= ROLLBACK SEGMENT CONTENTION ========================= If any ratio is > .01 then more rollback segments are needed NAME ... (1 Reply)
Discussion started by: jigarlakhani
1 Replies
Login or Register to Ask a Question