Shell script to apply functions to multiple columns dynamically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to apply functions to multiple columns dynamically
Prev   Next
# 1  
Old 11-10-2018
Shell script to apply functions to multiple columns dynamically

Hello,

I have a requirement to apply hashing algorithm on flat file on one or more columns dynamically based on header

sample input file
Code:
ID|NAME|AGE|GENDER
10|ABC|30|M
20|DEF|20|F

say if i want multiple columns based on the header example id,name or id,age or name,gender and hash and store the output value in a new column for each row

example
Code:
echo -n '10,abc'|md5sum =hashvalue

expected sample output:
Code:
ID|NAME|AGE|GENDER|HASHEDCOLUMNS|HASHVALUE
10|abc|30|M|ID,NAME|hashvalue
20|def|20|F|ID,NAME|hashvalue

or

Code:
ID|NAME|AGE|GENDER|HASHEDCOLUMNS|HASHVALUE
10|abc|30|M|ID,gender|hashvalue
20|def|20|F|ID,gender|hashvalue

I am having hard time visualizing this
I have written something but i dont know if its going to work or not really lost here.

Code:
#!/usr/bin/sh

target="id"   # here i m passing the header column to be md5 applied ideally this is parameterized and can send more than one column name seperated by comma "
target_idx=   # the column number of that field

{
  # reading header.
  IFS=, read -r -a header
  for idx in "${!header[@]}"; do  # and look for the target field in it
    [[ ${header[$idx]} = $target ]] && { target_idx=$idx; break; }
  done
  # then, iterate over lines
  while IFS=, read -r -a line; do
    a=echo-n ${line[$target_idx]}|md5sum # if my input in target is "id"
  done
} <testfile.txt

echo$line|"$a"

Thanks,
kathi

Last edited by RudiC; 11-11-2018 at 08:37 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print multiple required columns dynamically in a file using the header name?

Hi All, i am trying to print required multiple columns dynamically from a fie. But i am able to print only one column at a time. i am new to shell script, please help me on this issue. i am using below script awk -v COLT=$1 ' NR==1 { for (i=1; i<=NF; i++) { ... (2 Replies)
Discussion started by: balu1234
2 Replies

2. UNIX for Beginners Questions & Answers

How to apply the update statement in multiple servers on multiple dbs at a time .?

Hi , Can any please help the below requirement on all multiple servers and multiple dbs. update configuration set value='yes' ;1) the above statement apply on 31 Databases at a time on different Ip address eg : 10.104.1.12 (unix ip address ) the above ip box contains 4 db's eg : db... (2 Replies)
Discussion started by: venkat918
2 Replies

3. Shell Programming and Scripting

How can I apply 'date' command to specific columns, in a BASH script?

Hi everyone, I have a situation in which I have multiple (3 at last count) date columns in a CSV file (, delim), which need to be changed from: January 1 2017 (note, no comma after day) to: YYYY-MM-DD So far, I am able to convert a date using: date --date="January 12, 1990" +%Y-%m-%d ... (7 Replies)
Discussion started by: richardsantink
7 Replies

4. Shell Programming and Scripting

Read Two Columns - Apply Condition on Six other columns

Hello All, Here is my input univ1 chr1 100 200 - GeneA 500 1 0 0.1 0.2 0.3 0.4 0.5 univ1 chr1 100 200 - GeneA 600 1 0 0.0 0.0 0.0 0.0 0.1 univ1 chr1 100 200 - GeneA 700 1 0 0.4 0.4 ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

Creating IN list in PLSQL script dynamically by using shell script

Hi all, I have a PLSQL script which has a IN list where it takes some ids as input. For example SELECT * FROM EMPLOYEE WHERE EMPLOYEE_ID IN (comma separated list ) I want to run this quest inside a shell script but I would like to prepare the IN list dynamically where the employee ids... (1 Reply)
Discussion started by: LoneRanger
1 Replies

6. Shell Programming and Scripting

How to run multiple functions in Background in UNIX Shell Scripting?

Hi, I am using ksh , i have requirement to run 4 functions in background , 4 functions call are available in a case that case is also in function, i need to execute 1st function it should run in background and return to case and next i will call 2nd function it should run in background and... (8 Replies)
Discussion started by: karthikram
8 Replies

7. Shell Programming and Scripting

how to create the files dynamically in c shell script

how can i CREATE a txt file dynamically in c shell: for instance: #! /bin/csh for each i (*) cat>file$i.txt for each j do .... (1 Reply)
Discussion started by: jdsignature88
1 Replies

8. Shell Programming and Scripting

Shell script dynamically case in VAR

Hallo, I am working on a kdialog. This shall be able to load the required commands from a .conf file. First step runs good by loading the entries (selectabel entries) in a variable: MIRRORSELECT=$(kdialog --radiolist "Select your nearest mirror" $VAR1) The kdialog is accordingly correct... (2 Replies)
Discussion started by: ACTGADE
2 Replies

9. Shell Programming and Scripting

Managing dynamically multiple shell

I want to launch some shell scripts. I would have the possibility to change the number of shell scripts launched dynamically by modifying a variable, or a configuration file. For example, I start to launch 4 scripts at the same time, and after that, by modifying a variable, 6 scripts are... (0 Replies)
Discussion started by: gonzo38
0 Replies

10. Programming

reallocating structures dynamically in functions

I've recently started using structures, but I am having problems in allocating the structure dynamically. In the code below if i allocate the structure in the main program it works fine, and i get the expected output. However if i use the function rper below to increase the size of the structure i... (0 Replies)
Discussion started by: cezaryn
0 Replies
Login or Register to Ask a Question