views:

169

answers:

4

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?

+1  A: 

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

ssn
cat is not needed. cut -d ";" -f1 org_file.txt > newfile
ghostdog74
Cutting multiple fields leaves the delimiter in place between them.
Dennis Williamson
+4  A: 

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
paxdiablo
+3  A: 
#!/bin/bash

while read -d ';' ITEM; do
    echo "$ITEM"
done
John Kugelman
This is, to my knowledge, the only efficient way to do this using bash alone; i.e. without resorting to using an external program to do the parsing.
tylerl
A: 

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

Vereb