LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2019, 08:44 AM   #1
Ezzmazz
LQ Newbie
 
Registered: Apr 2019
Posts: 12

Rep: Reputation: Disabled
How to display the filename in process in my awk script ?


Hello,

I need again your help for a problem :

For my CGI in bash/html, I should display informations from many csv files like that :

http://www.noelshack.com/2019-32-4-1565270011-test4.png



The script that allow me to do that :
Code:
echo "<table>"
for fn in /var/www/cgi-bin/LPAR_MAP/*;
do
echo "<td>"
echo "<PRE>"
awk -F',|;' -v test="$test" '
     NR==1 { 
        split(FILENAME ,a,"[-.]");
      }
      $0 ~ test {
          if(!header++){
              print "DATE ========================== : " a[4] 
          }
          print ""
          print "RAM : " $5
          print "CPU 1 : " $6
          print "CPU 2 : " $7
          print "" 
          print ""
      }' $fn;

echo "</PRE>"
echo "</td>"
done
echo "</table>"
But I was in trouble with this script because this script display all the lines and I needed to not display the lines that are the same in each files.


So, I decide to change my script. My new script is :
Code:
awk -F',|;' '{ $0=$1","$5","$6","$7} /'$test'/ { if (!a[$0]++)
{
print "RAM : " $2
print "CPU 1 : " $3
print "CPU 2 : " $4
print ""
}
}' /var/www/cgi-bin/LPAR_MAP/*.csv
That script allow to display only the different lines in all my csv files. But, there is two things that I'm not able to integrate :
  1. split(FILENAME ,a,"[-.]") to keep what I need in the filename;
  2. $0 ~ test { if(!header++) { print "DATE ========================== : " a[4]}


    This is two of the important things in my script...

    Can you help me ?

    Thank you !
 
Old 08-08-2019, 09:18 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,053
Blog Entries: 3

Rep: Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952Reputation: 1952
You might try sub() instead and apply it to the built-in variable FILENAME or else to a copy of that variable.

Code:
 ... sub(".*/", "", FILENAME);  ...
 
Old 08-09-2019, 02:14 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,715

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
I am not seeing why you cannot integrate the 2 pieces you have mentioned?

1. The split will still perform the same action and if wanting to do it at the start of each file, simply use BEGINFILE section

2. You will need to reimplement the -v switch to use 'test' variable in script and reset header where appropriate (see above maybe?)


It is also unusual that your field positions have changed between the 2 scripts (ie. RAM $5 but is now $2 .... are you now reading from different files?)

It would seem you need to spend some time with the manual
 
1 members found this post helpful.
Old 08-10-2019, 11:21 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,160
Blog Entries: 11

Rep: Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084Reputation: 3084
@ Ezzmazz - You have posted multiple threads relating to the same basic script/problem, including this one, and this and this.

Please keep such related questions in a single thread in future. Doing so helps others to follow progress and changes more easily, making better use of their time and allowing better replies. It also helps future visitors by keeping related questions and replies in a single place.

To help keep the quality of questions and resulting answers useful for everyone, please review the Site FAQ, and in particular the Welcome page and the links it provides when posting questions in future.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
[SOLVED] Please help script loop into the /tmp/filename.txt out put with filename and wc. dotran Linux - Newbie 10 06-08-2012 05:02 PM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration