LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-27-2019, 09:26 AM   #1
Ezzmazz
LQ Newbie
 
Registered: Apr 2019
Posts: 12

Rep: Reputation: Disabled
Bash script to enumerate elements from csv files


Hello,

I allow me to writing here to seek your assistance about my script bash !


My csv file looks like :

Code:
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME2,LPAR_NAME2,12,0.8,1
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME3,LPAR_NAME3,0,null,0
FRAME_NAME3,LPAR_NAME3,0,null,0
FRAME_NAME3,LPAR_NAME3,0,null,0
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME2,LPAR_NAME2,25,1.0,2
But with 1115 lines.

I want to create a script that allow to display those informations like this :

Code:
FRAME_NAME 1 =======================
LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX


LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX


LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX

LPAR_NAME : LPAR_NAME1
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX

FRAME_NAME 2 =======================
LPAR_NAME : LPAR_NAME2
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX

LPAR_NAME : LPAR_NAME2
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX


FRAME_NAME3 =======================
LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX

LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX

LPAR_NAME : LPAR_NAME3
RAM : XXXXX
CPU 1 : XXXXX
CPU 2 : XXXXX
So it's just the name of the frame with below him, the list of the LPARS and his caracteristics that are in this frame.

I try to do this :

Code:
#!/bin/bash

OLDIFS=$IFS
IFS=","


while read FRAME LPARS RAM CPU1 CPU2
do
        echo -e "\e[1;33m$FRAME \
========================\e[0m\n\
        LPARS : \t$LPARS\n\
        RAM : \t$RAM\n\
        CPU1 : \t$CPU1\n\
        CPU2 : \t$CPU2\n"
done < my_csv_file.csv

But the result is an enumeration of every frames and every LPARS. However, I just want the first Frame with all thoses LPARS then the second frame with all thoses LPARS etc...

Can you show me how I can do this ?


Thanks you !
 
Old 06-27-2019, 09:53 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Sort the file first

See if this does what you want:

Code:
#!/usr/bin/bash

OLDIFS=$IFS
IFS=","


while read FRAME LPARS RAM CPU1 CPU2
do
        if [[ $FRAME != $PREV ]];then
                PREV="$FRAME"
echo -e "\e[1;33m$FRAME \
========================\e[0m\n"
        fi
echo -e "LPARS : \t$LPARS\n\
RAM : \t$RAM\n\
CPU1 : \t$CPU1\n\
CPU2 : \t$CPU2\n"
done < <(sort "$1")
 
Old 06-27-2019, 10:03 AM   #3
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
Your list seems to be of a consistent order and cleanly eliminated with commas. Here is a quick suggestion how this could be handled.

Code:
$ foo='FRAME_NAME1,LPAR_NAME1,25,1.0,2'
$ for i in a b c d e; do eval "$i=\"${foo%%,*}\""; foo="${foo#*,}"; done
$ echo "a = $a"
a = FRAME_NAME1
$ echo "b = $b"
b = LPAR_NAME1
$ echo "c = $c"
c = 25
$ echo "d = $d"
d = 1.0
$ echo "e = $e"
e = 2
 
Old 06-27-2019, 12:53 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
As demonstrated by crts this can be done in bash, but as the data is cleanly delimited, I would choose to use awk after passing through sort
 
Old 06-27-2019, 08:30 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME2,LPAR_NAME2,12,0.8,1
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME3,LPAR_NAME3,1,null,0
FRAME_NAME3,LPAR_NAME3,2,null,0
FRAME_NAME3,LPAR_NAME3,3,null,0
FRAME_NAME1,LPAR_NAME1,25,1.0,2
FRAME_NAME1,LPAR_NAME1,35,1.0,2
FRAME_NAME2,LPAR_NAME2,45,1.0,2
... this awk ...
Code:
awk -F "," '{t=substr($1,1,10)" "substr($1,11)
  a[t]=a[t]"\nLPAR NAME : "$2 \
           "\nRAM : "$3       \
           "\nCPU 1 : "$4     \
           "\nCPU 2 : "$5     \
           "\n"}              \
   END{for (j in a) print j " =============" a[j]"\n"}' \
$InFile >$OutFile
... produced this OutFile ...
Code:
FRAME_NAME 1 =============
LPAR NAME : LPAR_NAME1
RAM : 25
CPU 1 : 1.0
CPU 2 : 2

LPAR NAME : LPAR_NAME1
RAM : 25
CPU 1 : 1.0
CPU 2 : 2

LPAR NAME : LPAR_NAME1
RAM : 25
CPU 1 : 1.0
CPU 2 : 2

LPAR NAME : LPAR_NAME1
RAM : 35
CPU 1 : 1.0
CPU 2 : 2


FRAME_NAME 2 =============
LPAR NAME : LPAR_NAME2
RAM : 12
CPU 1 : 0.8
CPU 2 : 1

LPAR NAME : LPAR_NAME2
RAM : 45
CPU 1 : 1.0
CPU 2 : 2


FRAME_NAME 3 =============
LPAR NAME : LPAR_NAME3
RAM : 1
CPU 1 : null
CPU 2 : 0

LPAR NAME : LPAR_NAME3
RAM : 2
CPU 1 : null
CPU 2 : 0

LPAR NAME : LPAR_NAME3
RAM : 3
CPU 1 : null
CPU 2 : 0
Daniel B. Martin

.
 
Old 07-08-2019, 09:44 AM   #6
Ezzmazz
LQ Newbie
 
Registered: Apr 2019
Posts: 12

Original Poster
Rep: Reputation: Disabled
Hello !

I am sorry for the late reply and I thank you for your answers !


Quote:
Originally Posted by crts View Post
See if this does what you want:

Code:
#!/usr/bin/bash

OLDIFS=$IFS
IFS=","


while read FRAME LPARS RAM CPU1 CPU2
do
        if [[ $FRAME != $PREV ]];then
                PREV="$FRAME"
echo -e "\e[1;33m$FRAME \
========================\e[0m\n"
        fi
echo -e "LPARS : \t$LPARS\n\
RAM : \t$RAM\n\
CPU1 : \t$CPU1\n\
CPU2 : \t$CPU2\n"
done < <(sort "$1")
This script works perfectly. Can you just tell me what is the $PREV variable ? I don't understand how this if condition can display one frame and his informlations bellow him !

Thank you !
 
Old 07-08-2019, 10:16 AM   #7
individual
Member
 
Registered: Jul 2018
Posts: 315
Blog Entries: 1

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Ezzmazz View Post
Hello !

I am sorry for the late reply and I thank you for your answers !




This script works perfectly. Can you just tell me what is the $PREV variable ? I don't understand how this if condition can display one frame and his informlations bellow him !

Thank you !
This is an example of one of the oddities of Bash: variables magically come into scope. To make it less confusing you could set $PREV to an empty string before the loop. If you're wondering what $PREV's purpose is, though, it allows the script to only print the frame names it hasn't encountered before. Basically it allows you to group frame items together.
Code:
#!/usr/bin/bash

OLDIFS=$IFS
IFS=","

PREV=""
while read FRAME LPARS RAM CPU1 CPU2; do
    if [[ $FRAME != $PREV ]]; then
            PREV="$FRAME"
            echo -e "\e[1;33m$FRAME \
        ========================\e[0m\n"
    fi
    echo -e "LPARS : \t$LPARS\n\
    RAM : \t$RAM\n\
    CPU1 : \t$CPU1\n\
    CPU2 : \t$CPU2\n"
done < <(sort "$1")

Last edited by individual; 07-08-2019 at 10:18 AM. Reason: Reworded for clarity.
 
  


Reply



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
CSS for group of HTML elements in a series of such elements Turbocapitalist Linux - General 17 09-20-2017 12:16 AM
How to print lines in csv file if 1 csv column field = "text". There are 10 column (;) in csv file nexuslinux Linux - Newbie 9 04-22-2016 11:35 PM
In Javascript How to replace elements in one object with elements from another object pizzipie Linux - Software 1 05-08-2014 02:28 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM

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

All times are GMT -5. The time now is 07:37 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
Open Source Consulting | Domain Registration