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
# 8  
Old 07-16-2014
Quote:
Originally Posted by JITHENDER
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.
Please note the text marked in red above. You say you want every 4th line from your file. Every 4th line is marked in red. Then you say that you want to extract 4 values (one of which does not appear in the file at all) from those 12 lines.

mahi_mayu069 and RudiC have suggested ways to get the unique values from your output file; but that isn't what you requested. RudiC's code also strips off leading zeros; but you said you wanted to convert the 3rd and 4th characters of a 4 or more digit string treated as octal values to decimal.

I'm going to try once more before I give up on this thread:
  1. Why do you want an array containing 3, 5, 4, and 7 when 4 never appears in your data?
  2. If you want every 4th line from a file, why do you expect to get 4 values from a 12 line file?
  3. Do you really want to extract data from every 4th file from your list of files, or do you want to extract the digits between the 1st and 2nd periods in all of the filenames and process all distinct values you find.
  4. Why do you just want to look at the 3rd and 4th characters from the digits between the 1st and 2nd periods in your filenames?
  5. Why do you want an array? Why not just process the values as you find them?
# 9  
Old 07-17-2014
Quote:
Originally Posted by Don Cragun
Please note the text marked in red above. You say you want every 4th line from your file. Every 4th line is marked in red. Then you say that you want to extract 4 values (one of which does not appear in the file at all) from those 12 lines.

mahi_mayu069 and RudiC have suggested ways to get the unique values from your output file; but that isn't what you requested. RudiC's code also strips off leading zeros; but you said you wanted to convert the 3rd and 4th characters of a 4 or more digit string treated as octal values to decimal.

I'm going to try once more before I give up on this thread:
  1. Why do you want an array containing 3, 5, 4, and 7 when 4 never appears in your data?
  2. If you want every 4th line from a file, why do you expect to get 4 values from a 12 line file?
  3. Do you really want to extract data from every 4th file from your list of files, or do you want to extract the digits between the 1st and 2nd periods in all of the filenames and process all distinct values you find.
  4. Why do you just want to look at the 3rd and 4th characters from the digits between the 1st and 2nd periods in your filenames?
  5. Why do you want an array? Why not just process the values as you find them?

1.Why do you want an array containing 3, 5, 4, and 7 when 4 never appears in your data?
sorry it's a typo.

2.If you want every 4th line from a file, why do you expect to get 4 values from a 12 line file?
my program generate four instance so i get four values.

3.Do you really want to extract data from every 4th file from your list of files, or do you want to extract the digits between the 1st and 2nd periods in all of the filenames and process all distinct values you find.

I want to extract the digit for example i want to extract 0003 and convert to decimal 3 from this (/tmp/grepd.0003.00049:12:server_read_cb:xxld died)

4.Why do you just want to look at the 3rd and 4th characters from the digits between the 1st and 2nd periods in your filenames?

In 0003 i need decimal coverted 3.
Code:
cut -c 3,4

gives 03 and i convert to decimal using
Code:
val=03
echo "$0 + $val"|bc

don't confuse with code above code is just example.


5.Why do you want an array? Why not just process the values as you find them?

I've a for loop in my script and further processing of restarting of process based on these values. so i need array for temp storage of values and process it in loop.

Here I've two things
1. After extracting the value from the filename , covert to decimal

2. Store those values in array and process array in loop further restarting of process.

I hope u r clear now with above ansSmilie. plz ans how i can i store values in array and process them later.
# 10  
Old 07-17-2014
Did you try the proposal in post#7?
This User Gave Thanks to RudiC For This Post:
# 11  
Old 07-17-2014
If I correctly understand what you're trying to do (and I'm not convinced that I do), you can combine your script that produces your data file:
Code:
grep -rn "died" /tmp/grepd.0*|cut -d':' -f1|cut -d'.' -f2|cut -c 3,4

and RudiC's suggestion:
Code:
ARR=($(awk '/died/ {T[$2]} END {for (i in T) print i+0}' FS="." file))

into:
Code:
array=($(grep -rl "died" /tmp/grepd.0*|awk -F'[.]' '{T[$2]} END{for(i in T) print i+0}'))

or:
Code:
array=($(find /tmp/grepd.0* -type f|awk -F'[.]' '/died/{T[$2]} END{for(i in T) print i+0}'))

This should work both with ksh and bash. If you want to try either of these on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

What you do with array is up to you...
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 07-18-2014
Quote:
Originally Posted by Don Cragun
If I correctly understand what you're trying to do (and I'm not convinced that I do), you can combine your script that produces your data file:
Code:
grep -rn "died" /tmp/grepd.0*|cut -d':' -f1|cut -d'.' -f2|cut -c 3,4

and RudiC's suggestion:
Code:
ARR=($(awk '/died/ {T[$2]} END {for (i in T) print i+0}' FS="." file))

into:
Code:
array=($(grep -rl "died" /tmp/grepd.0*|awk -F'[.]' '{T[$2]} END{for(i in T) print i+0}'))

or:
Code:
array=($(find /tmp/grepd.0* -type f|awk -F'[.]' '/died/{T[$2]} END{for(i in T) print i+0}'))

This should work both with ksh and bash. If you want to try either of these on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

What you do with array is up to you...
yes this works fine.. But values are not coming in sorted order
# 13  
Old 07-18-2014
You didn't say anything about needing sorted output before (and you showed us one example where the output you said you wanted was not sorted). But, making several more very wild assumptions based on a pathname sample set of size 1, try:
Code:
array=($(ls /tmp/grepd.0*died|awk -F'[.]' 'BEGIN{p="x"}p!=$2{print (p=$2)+0}'))

This User Gave Thanks to Don Cragun 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