Script to complete all fields of template


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to complete all fields of template
# 1  
Old 10-01-2014
Question Script to complete all fields of template

Hello gurus;

I need to do a script to complete a technical template and I don't have idea to how to do Smilie I've try with bash and awk or sed but my brain is empty in this script. I need help please.

The incoming file is a csv with an index number, first occurence in the possition of the element, the number of line for the element, type of element and name of the element, this is an example of imput file:
Code:
1;0;1;Service;TEST Operating;;;;;;;;;;;;;;;;;;;
2;1;1;System;LoadBalancer_5;;;;;;;;;;;;;;;;;;;
3;2;1;System;HTTP_Server_4;;;;;;;;;;;;;;;;;;;
4;3;1;System;AS_KXKK1;;;;;;;;;;;;;;;;;;;
5;4;1;System;LDAP;;;;;;;;;;;;;;;;;;;
6;5;1;Component;LDAP;;;;;;;;;;;;;;;;;;;
7;4;2;Component;p_TEST;;;;;;;;;;;;;;;;;;;
8;4;3;Component;WASSRV5;;;;;;;;;;;;;;;;;;;
9;3;4;System;AS_KXKK2;;;;;;;;;;;;;;;;;;;
10;4;4;System;LDAP;;;;;;;;;;;;;;;;;;;
11;5;4;Component;LDAP;;;;;;;;;;;;;;;;;;;

This is an explanation of the first line of the imput file:
Code:
INDEX;POSSITION_IN_LINE;NUMBER_OF_LINE;TYPE_OF_ELEMENT;ELEMENT
1;0;1;Service;TEST Operating;;;;;;;;;;;;;;;;;;;

The idea is generate an output file with all type of elements and elements filled and if no previus element in the line is complete i need put the same element that is in the upper row:
Code:
1;Service;TEST Operating;System;Loadbalancer_5;System;HTTP_Server_4;System;AS_KXKK1;System;LDAP;Component;LDAP;;;;;;
2;Service;TEST Operating;System;Loadbalancer_5;System;HTTP_Server_4;System;AS_KXKK1;Component;p_krex;;;;;;;;
3;Service;TEST Operating;System;Loadbalancer_5;System;HTTP_Server_4;System;AS_KXKK1;Component;WASSRV5;;;;;;;;
4;Service;TEST Operating;System;Loadbalancer_5;System;HTTP_Server_4;System;AS_KXKK2;System;LDAP;Component;LDAP;;;;;;

The line one puts each possition, the first line is easy because I have all elements with the "NUMBER_OF_LINE" field but in the rest of lines I don't have idea that how complete with the rest of elements.

Please help me SmilieSmilieSmilie

I've attached two example files to try explain better that my english Smilie sorry for that.

May thats for your time.

Ren



Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks
# 2  
Old 10-01-2014
It is doable in pure bash and in awk.
Your attached files are in WinDOS format where awk provides an easy conversion.
Code:
awk -F\; '
{
# WinDOS to Unix
  sub("\r$","")
}
function printout() {
# assemble the line in the correct sequence
  line=nr
  for (i=0; (i in out); i++) line=line out[i]
  print line
}
($3!=nr) {
  if (NR>1) printout()
  nr=$3
}
{
# cut the first three fields
  i=$2
  sub("[^;]*;[^;]*;[^;]*","")
  out[i]=$0
}
END {
  if (NR>0) printout()
} 
' imput.txt

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 10-02-2014
Awesome MadeInGermany;

Only one comment, is possible fill only to up until the last ocurrence of line. I'll try to explain:

If the line number 2 only have "Component;P_TEST" in the possition 4, can I fill the line two without the fields beyond the last element of the line? I only need fill the elements before the last element of each line.

For instance:

Imput line for line 2:
Code:
7;4;2;Component;p_TEST

I need the output

Code:
2;Service;TEST Operating;System;Loadbalancer_5;System;HTTP_Server_4;System;AS_KXKK1;Component;p_TEST;;;;;;;;

Instead of

Code:
2;Service;TEST Operating;System;Loadbalancer_5;System;HTTP_Server_4;System;AS_KXKK1;Component;p_TEST;Component;LDAP;;;;;;;

Thanks a lot for your support SmilieSmilieSmilie

Ren
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tip: template for a safe and portable script

In an attempt to finally end this article I start this new thread. Here is a template for a safe and portable script. #!/bin/bash # /bin/bash exists on most still supported Unixes # # tr and date behave better with if ; then export LC_ALL=C; else export LANG=C; fi # # Unix optional packages... (2 Replies)
Discussion started by: MadeInGermany
2 Replies

2. Shell Programming and Scripting

Script to create EVIM template with SAS extension

I write lots of SAS programs and would like to create a script that allows me to have a template each time I create a new program file. Specs: I use EVIM for my editor. I run SAS in batch mode. We use RedHat 6. I don't use c shell. I want a script that will do the following: >... (3 Replies)
Discussion started by: starbecks
3 Replies

3. Programming

Perl script to create latex template.

Hi, I have XML file and I extracted some tags and stored in hash, my data as look like this $var1={ 'stud.xml'={ '24'=>'<address> <streetname="xxxx"/> <housenum="138"/"> ... (9 Replies)
Discussion started by: veerubiji
9 Replies

4. Shell Programming and Scripting

Script template for inputting filenames and print results

Hi, Hope you are all well. New to scripting, and all those characters are all a new language for me. Though hoping to get my little head round it all sooner or later. I was wondering whether anyone could help with a script template example. What I would like to happen is to run the script... (8 Replies)
Discussion started by: loky27
8 Replies

5. Infrastructure Monitoring

Zabbix Template and PERL Script for Monitoring Apache2

Hello, Kindly find attached a copy of the Zabbix template and PERL script we are using to monitor our Apache2 server. Here are the entries for zabbix_agentd.conf UserParameter=apache2.total_accesses,/etc/zabbix/zabbix_apache2.pl|cut -f1 -d":"... (4 Replies)
Discussion started by: Neo
4 Replies

6. Shell Programming and Scripting

Expect script does not complete

I am new to Expect and need an expect script to expect data from a telnet session and respond with options as written in the script. There is no human input required. Problem is it gets to a point in the script and will just end. I have tried adjusting 'set timeout' and still does not help. The... (0 Replies)
Discussion started by: NQReady
0 Replies

7. UNIX for Dummies Questions & Answers

BASH complete-filename & menu-complete together

Hi, Does anyone know how to make BASH provide a list of possible completions on the first tab, and then start cycling through the possibilites on the next tab? Right now this is what I have in my .bashrc: bind "set show-all-if-ambiguous on" bind \\C-o:menu-complete This allows... (0 Replies)
Discussion started by: Mithu
0 Replies
Login or Register to Ask a Question