To write shell script based on 1 and 4th columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To write shell script based on 1 and 4th columns
# 1  
Old 05-06-2014
To write shell script based on 1 and 4th columns

Hi,
I am trying to write shell script for below requirement
File has below values
Code:
  @mqm    soft    nproc     75000
  @mqm    hard    nproc     75000
  @mqm    soft    nofiles         76492
  @mqm    hard    nofiles         76492
  @mqm    soft    locks           76492
  @mqm    hard    locks           76492

I am writing shell script to achive if 1st column @mqm and 2,3rd columns are conditions then 4th columns values should check. If 4th columns values is less than above mentioned value then it should change to above mentioned value. If 4th column value is greater than above mention value then it should leave it.

When i run the script it should do above checks.
For example. let us ake First row
1st columns (@mqm) should check 2nd column (soft) then 3rd column(npproc) if it is pattern then it should check for 4th column value(75000). As 4th column value is Equal to 75000 then it should leave the value as it is if less than 75000 then it should change to 75000, if it is greater than 75000 (assume 80000) then it should leave as it is to 85000

Can you help me in achiveing or writing this script
# 2  
Old 05-06-2014
If this is the benchmark data, provide us the input data that needs tobe verified with this

---------- Post updated at 10:00 AM ---------- Previous update was at 09:57 AM ----------

Code:
awk 'NR == FNR{a[$1 $2 $3] = $4; next}
  {if($4 < a[$1 $2 $3])
    $4 = a[$1 $2 $3]
  print $0}' benchmark_file input_file

# 3  
Old 05-06-2014
Hi SriniShoo,

It is like hard coding, there won't be any input data. I am looking for script which checks the file and conditions. Based on conditions it should change the value.

assume script name is change.sh
when i run ./change.sh it will check the file which has given values in my first post(Assume file name is values.txt)

it should check 1st,2nd,3rd values (If you observe 2nd and 3rd values gets change). So if 1st,2nd,3rd values are (@mqm soft npproc) then it should check 4th column values is less than 75000 or not if yes it should change to 75000, if above 75000 it should leave.

Similarly for all 6 rows

Or else let me know with out hard coding is there any way to achive this
# 4  
Old 05-06-2014
without seeing a desired output based on a sample input - just guessing....
Code:
awk '{idx=$1 $2 $3} FNR==NR{a[idx]=($4>a[idx])?$4:a[idx];next}{ $4=a[idx}1' values.txt values.txt

# 5  
Old 05-06-2014
Somehow, I am unable to understand your requirement clearly
If you are only concerned about (@mqm soft npproc), below code will suffice
If your requirement is different, please provide proper input, along with expected output
Code:
awk '($1 " " $2 " " $3) == "@mqm soft npproc" {if($4 < 75000) $4 = 75000}1' file

# 6  
Old 05-07-2014
Ok.. Let me explain you in detail. I have multiple unix servers and i have a file (security.txt) on all the servers. Security files will have values like below.
file: security.txt on 1.2.3.4
Code:
@mqm    soft    nproc     75000
@mqm    hard    nproc     85000
@mqm    soft    nofiles         76492
@mqm    hard    nofiles         86492
@mqm    soft    locks           66492

On all the servers security.txt file will not have same values. On few servers 4th Columns values will be lesser than standard values or greater than standard values.
Below are the standard values of security.txt
Code:
@mqm    soft    nproc     75000
@mqm    hard    nproc     75000
@mqm    soft    nofiles         76492
@mqm    hard    nofiles         76492
@mqm    soft    locks           76492
@mqm    hard    locks           76492

1.If 4th column values are lesser than standard values then when i run the script it should change to standard values
2.If 4th colums values are greater than standard values then it should leave the values as it is as those are greater than standard values.
3.If any of the row is not there in file then it should add that row in security.txt when i run the script (script.sh)

When i run the script (script.sh) on 1.2.3.4 expected output is like below
Code:
@mqm    soft    nproc     75000
@mqm    hard    nproc     85000 (Left this values as it is greater than standard value 75000)
@mqm    soft    nofiles         76492 
@mqm    hard    nofiles         86492 (Left this values as it is greater than standard value 76492)
@mqm    soft    locks           76492 (Changed this values to 76492 as earlier values is 66492)
@mqm    hard    locks           76492 (This row is added as this is not there in security.txt on 1.2.3.4)

# 7  
Old 05-07-2014
create a 'standard.txt' file with the values you think are standard.
Code:
$ cat standard.txt
@mqm    soft    nproc   75000
@mqm    hard    nproc   75000
@mqm    soft    nofiles 76492
@mqm    hard    nofiles 76492
@mqm    soft    locks   76492
@mqm    hard    locks   76492
$

Now, you have a 'security.txt' file as below
Code:
$ cat security.txt
@mqm    soft    nproc   75000
@mqm    hard    nproc   85000
@mqm    soft    nofiles 76492
@mqm    hard    nofiles 86492
@mqm    soft    locks   66492
$

Run the below code
Code:
awk 'BEGIN {FS = OFS = "\t"}
  NR == FNR {a[$1 FS $2 FS $3] = $4;
  next}
  {if($4 < a[$1 FS $2 FS $3])
    {$4 = a[$1 FS $2 FS $3]}
  print
  delete a[$1 FS $2 FS $3]}
  END {for(x in a)
    {print x, a[x]}}' standard.txt security.txt

Below is the result when I ran the code on the files
Code:
$ awk 'BEGIN {FS = OFS = "\t"}
>   NR == FNR {a[$1 FS $2 FS $3] = $4;
>   next}
>   {if($4 < a[$1 FS $2 FS $3])
>     {$4 = a[$1 FS $2 FS $3]}
>   print
>   delete a[$1 FS $2 FS $3]}
>   END {for(x in a)
>     {print x, a[x]}}' standard.txt security.txt
@mqm    soft    nproc   75000
@mqm    hard    nproc   85000
@mqm    soft    nofiles 76492
@mqm    hard    nofiles 86492
@mqm    soft    locks   76492
@mqm    hard    locks   76492
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

2. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. Shell Programming and Scripting

Need to write a shell script

Hi, I need to write a statement which will read a data from a specific line based on condition and then read related lines to delete. For example: |T20150322 100930290 208940000 598000080 700000000 930000202 100000000 .T56789 |T20150322 0100000000 0200000000 0500000000 9000000000... (1 Reply)
Discussion started by: abhi.mit32
1 Replies

4. Shell Programming and Scripting

Python Script Write to Columns

Hi there, Am tasked with writing a script to give a similar output like this every 5 minutes: Total_PDP WEB_PDP BB_PDP WEB_AVAI_IP BB_AVAIL_IP Oct 9 15:06 2014 2000 1000 1000 200 400 Any idea on how to go about this, I have not done something like this... (1 Reply)
Discussion started by: infinitydon
1 Replies

5. Shell Programming and Scripting

awk script to split file into multiple files based on many columns

So I have a space delimited file that I'd like to split into multiple files based on multiple column values. This is what my data looks like 1bc9A02 1 10 1000 FTDLNLVQALRQFLWSFRLPGEAQKIDRMMEAFAQRYCQCNNGVFQSTDTCYVLSFAIIMLNTSLHNPNVKDKPTVERFIAMNRGINDGGDLPEELLRNLYESIKNEPFKIPELEHHHHHH 1ku1A02 1 10... (9 Replies)
Discussion started by: viored
9 Replies

6. UNIX for Dummies Questions & Answers

Write 2nd and 3rd fields to a 4th file name?

I have a flatfile A.txt date|products|notes|location 121117|a108|this is a test|florida 121118|b111|just test it|tampa How do i write an awk to create a file name as location.txt and have products:notes awk -F'|' '{ print $2 ":" $3 }' A.txt > $4.txt I am sure it cannot write to... (5 Replies)
Discussion started by: sabercats
5 Replies

7. Shell Programming and Scripting

awk based script to ignore all columns from a file which contains character strings

Hello All, I have a .CSV file where I expect all numeric data in all the columns other than column headers. But sometimes I get the files (result of statistics computation by other persons) like below( sample data) SNO,Data1,Data2,Data3 1,2,3,4 2,3,4,SOME STRING 3,4,Inf,5 4,5,4,4 I... (9 Replies)
Discussion started by: ks_reddy
9 Replies

8. Shell Programming and Scripting

awk based script to find the average of all the columns in a data file

Hi All, I need the modification for the below mentioned code (found in one more post https://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html) to find the average values for all the columns(but for a specific rows) and print the averages side by side. I have... (4 Replies)
Discussion started by: ks_reddy
4 Replies

9. Shell Programming and Scripting

Awk based script to find the median of all individual columns in a data file

Hi All, I have some data like below. Step1,Param1,Param2,Param3 1,2,3,4 2,3,4,5 2,4,5,6 3,0,1,2 3,0,0,0 3,2,1,3 ........ so on Where I need to find the median(arithmetic) of each column from Param1...to..Param3 for each set of Step1 values. (Sort each specific column, if the... (5 Replies)
Discussion started by: ks_reddy
5 Replies

10. Shell Programming and Scripting

Need to Write Shell Script based off of this shell command

I'm trying to read a bunch of log files and output the lines that contain particular strings. To accomplish this, I've been running the following from the command line: find . -name "*" | xargs grep " " | grep " " > output.txt Two grep statements are needed in case I'm looking for a... (3 Replies)
Discussion started by: Rally_Point
3 Replies
Login or Register to Ask a Question