---
title: "A Sample Bash Script: Reading, Transforming and Writing to Files"
description: "The content describes a Bash script using 'sed' command to modify input file data and write output to multiple files."
url: https://srinimf.com/2023/11/05/a-sample-bash-script-reading-transforming-and-writing-to-files/
date: 2023-11-05
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/11/pexels-photo-1266302.jpeg?fit=1880%2C1253&ssl=1
categories: ["Linux"]
tags: ["Files in Bash Script", "Shell script"]
type: post
lang: en
---

# A Sample Bash Script: Reading, Transforming and Writing to Files

Data transformation is a crucial step that can be implemented in a shell script, using the [**sed utility**](https://srinimf.com/2021/11/08/how-to-search-and-replace-in-bash-easily-using-sed/) [to read](https://srinimf.com/2021/10/23/bash-script-simplified-how-to-read-array-and-print/) and transform an input file. After transformation, it writes to output files.

## Importance of data transformation

Data transformation in Linux involves converting data from one format to another to make it more useful. This process includes manipulating files and text, which helps users write better scripts.

![Bash script](https://i0.wp.com/srinimf.com/wp-content/uploads/2023/11/bash-scripting-files.jpg?resize=2240%2C1260&ssl=1)*Bash script files*

## Real-world examples of data transformation using ‘sed’

In sed, some real-world examples of data transformation include:

**Replacing text**

To replace text in a file using Sed, you can use the following command. It will replace all occurrences of “old text” with “new text” in the specified file.

```
Command: sed 's/apple/orange/' input.txt
```

**Deleting lines**

Sed can be used to delete lines from a file. For example, deleting all lines containing the word “delete” in a file can be done using the command.

```
Command: sed '/pattern/d' input-file > output-file
```

**Inserting text**

Sed can be used to insert text into a file. For example, inserting the text “new line” before every occurrence of the word “insert” in a file can be done using the command.

```
Command: sed '/pattern/i\text-to-insert' input-file > output-file
```

**Rearranging text**

Sed can be used to rearrange the order of text in a file. For example, swapping the position of two words in a file can be done using the command.

```
Command: echo "Hello, World" | sed 's/\(.*\), \(.*\)/\2, \1/'
```

**Extracting data**

Sed can be used to extract data from a file. For example, removing all email addresses from a file can be done using the command.

```
Command: grep -E -o '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b' input.txt
```

## Bash Script: Reads input, and writes transformed data to output files

Below is an example script that uses the [**Sed utility**](https://srinimf.com/2015/04/19/unix-sed-and-awk-commands/) to replace all occurrences of the word ‘foo’ with ‘bar’ globally.

```
#!/bin/bash

# Read data from input file
input_file="input.txt"
data=$(cat $input_file)

# Transform the data
transformed_data=$(echo $data | sed 's/foo/bar/g')

# Write transformed data to multiple files
output_dir="output"
mkdir -p $output_dir

# Write transformed data to file1.txt
file1="$output_dir/file1.txt"
echo $transformed_data > $file1

# Write transformed data to file2.txt
file2="$output_dir/file2.txt"
echo $transformed_data > $file2

# Write transformed data to file3.txt
file3="$output_dir/file3.txt"
echo $transformed_data > $file3

echo "Data transformation and file writing completed!"
```

## Step-by-step explanation

The script reads the content of an input file using the “cat” command. It then applies transformations using the “sed” command and stores the result in the “transformed_data” variable.

The `output_dir` variable decides the folder where the output files are stored. If the folder doesn’t exist, it will be created automatically. The paths of the output files (file1, file2, and file3) are specified within the `output_dir`.

The transformed data is written to each output file using the “>” operator. Finally, a message indicating the completion of the data transformation and file writing is printed.

Conclusion

You can modify the script to suit your specific needs, including changes to the transformation process or the number and names of output files. The tips provided can help improve your ability [to write](https://srinimf.com/2017/05/02/unix-script-examples/) scripts.

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F11%2F05%2Fa-sample-bash-script-reading-transforming-and-writing-to-files%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F11%2Fpexels-photo-1266302.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=A%20Sample%20Bash%20Script%3A%20Reading%2C%20Transforming%20and%20Writing%20to%20Files)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/11/05/a-sample-bash-script-reading-transforming-and-writing-to-files/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/11/05/a-sample-bash-script-reading-transforming-and-writing-to-files/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/11/05/a-sample-bash-script-reading-transforming-and-writing-to-files/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/11/05/a-sample-bash-script-reading-transforming-and-writing-to-files/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20A%20Sample%20Bash%20Script%3A%20Reading%2C%20Transforming%20and%20Writing%20to%20Files&body=https%3A%2F%2Fsrinimf.com%2F2023%2F11%2F05%2Fa-sample-bash-script-reading-transforming-and-writing-to-files%2F&share=email)
-

### Like this:

Like Loading…
