Hi

I am trying to write a script in Bash Shell. I have the file test.txt and has the following data:

AIR DR
[ 4] Pressure :50
AIR UR
[ 3] Pressure :70
WATER DR
[ 3] Pressure :90
[ 3] PIS: 402ps
WATER UR
[ 3] Pressure :110
[ 3] PIS: 402ps


Now i want to store each category's data in separate file.. For example :

AIR DR
[ 4] Pressure :50

in AIR DR.txt file

and simliarly the rest of the data in 3 other files.

I am able to read the file but I am not able to store them in different files.
Here is what I did to read the file:

while read line
do
 	echo $line >> line.txt
done <results.txt

Please help I am new to bash scripting!..

You can use a case statement to get what you are after. For example:

#!/bin/bash

LOG=/dev/null

while read line; do
    case ${line} in
        'AIR DR')
        LOG='air_dr.out'
        ;;
        'AIR UR')
        LOG='air_ur.out'
        ;;
        'WATER DR')
        LOG='water_dr.out'
        ;;
        'WATER UR')
        LOG='water_ur.out'
        ;;
        *)
        echo ${line} >> ${LOG}
        ;;
    esac
done < results.txt
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.