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

Dani AI

Generated

Good start, — your while read loop writes every line into the same file. ’s case-based answer works but requires you to list every category manually. A more maintainable approach is to detect the category header lines and create a file from each header name, then append the following data lines to that file. The awk solution below does this dynamically and sanitizes filenames.

awk '
{ sub(/\r$/, "") }                     # strip Windows CR if present
/^[[:space:]]*$/ { next }              # skip blank lines
/^\[/ {                                 # data lines that start with [
    if (fname == "") fname = "unknown"
    print $0 >> (fname ".txt")          # append data to current file
    next
}
{                                       # otherwise treat line as header
    fname = $0
    gsub(/ /, "_", fname)               # spaces -> underscores
    gsub(/[^A-Za-z0-9_.-]/, "", fname)  # remove unsafe chars
    print $0 > (fname ".txt")           # write header (overwrite file)
}
' results.txt

Notes and troubleshooting:

  • Header lines become filenames (spaces -> underscores). Existing files with the same name are overwritten because the header is written with >; change that to >> if you prefer always appending.
  • If a file contains Windows CRLF endings, the sub(/\r$/,"") line prevents stray \r from polluting filenames.
  • If headers are more complex than plain lines (prefixes, timestamps), adjust the header-detection rule. If the input might contain data lines before any header, the script writes them to unknown.txt.
  • To start fresh, remove or move target .txt files before running to avoid mixing old and new data.

This keeps the logic data-driven (no hardcoded categories) and is simpler to maintain than a long case list.

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.