Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

printing values from file

Hi
I have these  8 columns in file attacehd
 separated by pipe to be  emailed as report.Each column in
the file separated has to be given a name and printed to file email_file.txt and emailed
as shown

field name     values
-----------    -------
 Field1          I,MM,O,P,V
 Field2          00,10,17 ,etc
 Field3
  ---
 Field8          F, P  

mailx -s "subjcet" -c  $tempMail < email_file.txt
0
uco
Asked:
uco
  • 5
  • 4
  • 3
1 Solution
 
farzanjCommented:
Is it possible to show how the files look like. Change the values for security.
0
 
farzanjCommented:
Here is a script to do it
#!/bin/bash  #Or use ksh

file=$(<FILENAME)
for ((i=1;i<=8;i++))
do
   printf "Field $i: "
   echo $file | cut -d'|' -f1
   file=$(echo $file | cut -d'|' -f2-)
done > email_file.txt

Open in new window


Try this first.  If it works, email with the command you mentioned.
0
 
woolmilkporcCommented:
Or awk:

 
awk '{split($0,A,"|"); for(i=1;i<=8;i++) B[i]=B[i] A[i] ","} END {for(j=1;j<i;j++) print "Column#" j, B[j]}' inputfile | sed 's/,$//' | tee email_file.txt | mailx -s "Values" uco@domain.tld

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ucoAuthor Commented:
Hi
It is close but
Two changes  needed  
I dont want  commas if there are no more values in a column
Column names are not  seqeuntial, they are different

Place
City
Modifier
Code
Type
Risk
Eval
Hype

thanks
0
 
farzanjCommented:
Which one do you want to use?  The first one or awk?
0
 
ucoAuthor Commented:
sorry! The first one is not working, I am using the awk one
0
 
woolmilkporcCommented:
OK:

awk 'BEGIN {NAMES="Place City Modifier Code Type Risk Eval Hype"; F=split(NAMES,N," ")}
     {split($0,A,"|"); for(i=1;i<=F;i++) B[i]=B[i] A[i] ","}
     END {for(j=1;j<i;j++) printf "%-10s%s\n", N[j], B[j]}' inputfile | sed 's/,*$//'  \
     | tee email_file.txt | mailx -s "Values" uco@domain.tld

Open in new window

0
 
ucoAuthor Commented:
Hi
Works great
Appreciate if you could get them in this style  with header Field Name and Values.
If a column has more than 30 values it goes to next line  as shown below. Each line dont have more than 30 values

field name          values
-----------             -------
 Field1             I,MM,O,P,V,-----30
                        31,32,33 -------60

 Field2              00,10,17 ,etc
 Field3
  ---
 Field8               F, P  
0
 
woolmilkporcCommented:
A word and a blow.  
awk 'BEGIN {NAMES="Place City Modifier Code Type Risk Eval Hype"; F=split(NAMES,N," ")}
     {split($0,A,"|"); for(i=1;i<=F;i++) {if(NR%30==0) A[i]=A[i] ",\n              ";
         else A[i]=A[i] ","; B[i]=B[i] A[i]}}
     END { {printf "%-14s%s\n%-14s%s\n", "Field Name", "Values", "----------", "------"}
         {for(j=1;j<=F;j++) printf "%-14s%s\n\n", N[j], B[j]}}' inputfile \
     | sed 's/,*$//' | egrep -v "^[ ]*$" \
     | tee email_file.txt | mailx -s "Values" uco@domain.tld

Open in new window

wmp
0
 
ucoAuthor Commented:
Great ! what could I Say, Simply Suerb, Is there  way in expert exchange I can directly assign quetion to you?
0
 
woolmilkporcCommented:
Glad you like it!

Do you mean asking questions which only a particular member can see and answer?

If so - No, that's not possible, and it's also pointless, IMO.
There are lots of fine experts around here, why should you voluntarily want to go without their expertise?

Rather take care to place your questions into the right zones.
Cross-posting is not forbidden here, it's even desired in many cases.
So consider selecting not only "Unix" but additionally your particular OS flavor (or/and programming language, if desired).
Don't worry, I will find any Unix, Linux, Scripting etc. questions.
See my zone ranking, so you will see my areas of interest/knowledge.

Good luck and have fun!

wmp

0
 
ucoAuthor Commented:
Great Work, to the point, Simply Superb
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now