0

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!..

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by L7Sqr
1

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.