Morgan Forever
Morgan Forever

Reputation: 117

Print a column from a CSV files in Linux

I have a very stupid question which I thought I could answer myself easyly...well I'm not able.

I have a file ";" separated as following:

Grand Hotel  ;Yes;No availability;Dus ;11; . 130,00 ;No availability;500 mt from Arena;

I have to print it in a file (to move it into Excel) like following:

Grand Hotel
Yes
No Availability
...etc etc

So simple, I just can't find a very easy solution. Since i'm working on a Debian Bash shell environment, I'm trying anything (bash script, awk, grep)...nothing works. Please Help. M.

Upvotes: 1

Views: 667

Answers (3)

glenn jackman
glenn jackman

Reputation: 247210

Assuming that your file is longer than one line, what will be the new record separator when all the fields are in separate lines?

This piece of awk will write each field on a new line, and add a blank line between each record.

awk 'BEGIN {FS=";"; OFS="\n"; ORS="\n\n"} {$1=$1} 1'

The odd $1=$1 bit is required to force awk to re-write $0 (i.e. the current record) with the new separators.

example:

$ awk 'BEGIN {FS=";"; OFS="\n"; ORS="\n\n"} {$1=$1} 1' << END
a;b;c
d;e;f
END

outputs

a
b
c

d
e
f

Upvotes: 1

Martin T&#246;rnwall
Martin T&#246;rnwall

Reputation: 9599

So basically you want to convert a file with lines of the form F1;F2;F3;...;FN into:

 F1
 F2
 F3
 ...
 FN

We can use tr(1) to translate the semicolons into line breaks:

tr ';' '\n' < infile

Where "infile" is the name of the CSV input file.

Upvotes: 3

cnicutar
cnicutar

Reputation: 182754

How about sed:

echo '....' | sed 's/;/\n/g'

Upvotes: 1

Related Questions