2

I have one question about my script.

I have this kind of csv file to process with my script :

RXK7;MIAO24;Running;Linux;24;16;32;DefaultPool;shared
RXK6;MIAO24;Running;Linux;24;16;32;DefaultPool;shared
RXK4;MIAO24;Running;Linux;24;16;32;DefaultPool;shared
RXK1;MIAO24;Running;Linux;24;16;32;DefaultPool;shared
RXK2;MIAO24;Running;Linux;24;16;32;DefaultPool;shared
RXK5;MIAO24;Running;Linux;24;16;32;DefaultPool;shared
RXK3;MIAO24;Running;Linux;24;16;32;DefaultPool;shared

I want to keep only the 1,2,5,6 and 7 columns. To do that, I use this command :

cat test.csv | awk -F',|;' '{print $1","$2","$5","$6","$7}'

And the result is :

RXK7,MIAO24,24,16,32
RXK6,MIAO24,24,16,32
RXK4,MIAO24,24,16,32
RXK1,MIAO24,24,16,32
RXK2,MIAO24,24,16,32
RXK5,MIAO24,24,16,32
RXK3,MIAO24,24,16,32

With my script :

#!/bin/bash

OLDIFS=$IFS
IFS=','


for arg
do

    echo "File :" $arg | cut -d'.' -f1
    echo "======================================================="
    echo ""

    while read FRAME LPARS RAM CPU1 CPU2
    do
        if [[ $FRAME != $PREV ]]
        then
            PREV=$FRAME
            echo "FRAME : $FRAME"
            echo -e "-----------------\n"
        fi
        echo -e "LPARS :\t$LPARS\n\
RAM : \t$RAM\n\
CPU 1 :\t$CPU1\n\
CPU 2 :\t$CPU2\n"
        echo ""
    done < "$arg"
done

I can display theses informations of my differents csv like that :

File : test2
=======================================================

LPARS : 
RAM :   
CPU 1 : 
CPU 2 : 


FRAME : RXK7
-----------------

LPARS : MIAO24
RAM :   24
CPU 1 : 16
CPU 2 : 32


FRAME : RXK6
-----------------

LPARS : MIAO24
RAM :   24
CPU 1 : 16
CPU 2 : 32


FRAME : RXK4
-----------------

LPARS : MIAO24
RAM :   24
CPU 1 : 16
CPU 2 : 32


FRAME : RXK1
-----------------

LPARS : MIAO24
RAM :   24
CPU 1 : 16
CPU 2 : 32


FRAME : RXK2
-----------------

LPARS : MIAO24
RAM :   24
CPU 1 : 16
CPU 2 : 32


FRAME : RXK5
-----------------

LPARS : MIAO24
RAM :   24
CPU 1 : 16
CPU 2 : 32


FRAME : RXK3
-----------------

LPARS : MIAO24
RAM :   24
CPU 1 : 16
CPU 2 : 32

But this is possible only if I create a temporary file beforehand :

cat test.csv | awk -F',|;' '{print $1","$2","$5","$6","$7}' > test2.csv

I want to know if it's possible to do this without the need to create a temporary file ? I think something like :

for arg
do

    echo "File :" $arg | cut -d'.' -f1
    echo "======================================================="
    echo ""

    cat $arg | awk -F',|;' '{print $1","$2","$5","$6","$7}' | while read FRAME LPARS RAM CPU1 CPU2
    do
        if [[ $FRAME != $PREV ]]
        then
            PREV=$FRAME
            echo "FRAME : $FRAME"
            echo -e "-----------------\n"
        fi
        echo -e "LPARS :\t$LPARS\n\
RAM : \t$RAM\n\
CPU 1 :\t$CPU1\n\
CPU 2 :\t$CPU2\n"
        echo ""
    done < "$arg"
done

Or maybe it's possible to do this only with awk ?

Thank you for your help !

2
  • 1
    Personally, I would do it with just awk, since awk can also print \t and \n. Or I would use Perl :) . Anyway, if you remove the < "$arg" after done, I think the code you have might actually work. Edit and in the awk code, change "," to " ". Commented Jul 12, 2019 at 10:11
  • It is very easy to do with awk alone awk -F',|;' '{ print "\nFRAME:",$1; print "==================="; print "\nLPARS:",$2; print "\nRAM:",$5; print "\nCPU1:",$6; print "\nCPU:",$7 }' input.txt Commented Jul 12, 2019 at 10:17

1 Answer 1

2

You can use dummy read values, so you don't have to cut your columns:

while read FRAME LPARS _ _ RAM CPU1 CPU2
do
    # Things
done < "$arg"

You can also use the code you provided. Is it not working?

Also, I would advise you to use cut instead of awk for those simple situations, as cut is a lot lightweigth than awk:

cut -d; -f1,2,5-7 test.csv

And clearly, it is possible and easier to do only with awk.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by " dummy read values ? " I can do : while read FRAME LPARS _ _ RAM CPU1 CPU 2; do ###; done <"$arg" And the " _ _ " are the 3 and 4 columns ?
Yes. Those placeholders will take the values that you are not interested in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.