Biswajyoti Das
Biswajyoti Das

Reputation: 8301

split a file into segments?

I have a file containing text data which are separated by semicolon ";". I want to separate the data , in other words split where ; occurs and write the data to an output file. Is there any way to do with bash script?

Upvotes: 0

Views: 1188

Answers (4)

Vereb
Vereb

Reputation: 14716

You can translate a character to another character by the 'tr' command.

cat input.txt | tr ';' '\n' > output.txt

Where \n is new line and if you want a tab only you should replace it with \t

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881113

You most likely want awk with the FS (field separator variable) set to ';'.

Awk is the tool of choice for column-based data (some prefer Perl, but not me).

echo '1;2;3;4;5
6;7;8;9;10' | awk -F\; '{print $3" "$5}'

outputs:

3 5
8 10

If you just want to turn semicolons into newlines:

echo '1;2;3;4;5
6;7;8;9;10' | sed 's/;/\n/g'

outputs the numbers 1 through 10 on separate lines.

Obviously those commands are just using my test data. If you want to use them on your own file, use something like:

sed 's/;/\n/g' <input_file >output_file

Upvotes: 4

John Kugelman
John Kugelman

Reputation: 361565

#!/bin/bash

while read -d ';' ITEM; do
    echo "$ITEM"
done

Upvotes: 3

ssn
ssn

Reputation: 2897

Try:

cat original_file.txt | cut -d";" -f1 > new_file.txt

This will split each line in fields delimited by ";" and select the first field (-f1). You can access other fields with -f1, -f2, ... or multiple fields with -f1-2, -f2-.

Upvotes: 1

Related Questions