nicetry
nicetry

Reputation: 177

How to sed the first/last ocurrence of a row in a CSV?

Hello I have a CSV file with this random structure

,,,,,VALUE1,,,,,VALUE2,,,,
,,VALUE3,,,VALUE4,,,VALUE5,
,,,,,,,,,,,,,,,,,,,
,,,,,,,VALUE6,,,,VALUE7,,
,,,,,,,,VALUE8,,,,,,

And I want to output the first and last ocurrence of each line.

in this manner

For first ocurrence

VALUE1
VALUE3
(empty)
VALUE6
VALUE8

for last ocurrence

VALUE2
VALUE5
(empty)
VALUE7
VALUE8

I've searched for hours and didn't find the best way to achieve this.

Thank you very much.

EDIT: Thank you everybody, this ,+ as separator is the key.

Upvotes: 0

Views: 70

Answers (5)

Jasen
Jasen

Reputation: 12412

if you wanr (empty) as part of the output.

first:

sed '
 s/,*\([^,]\+\).*/\1/;
 s/^,*$/(empty)/;
'

last:

sed '
 s/\(^\|.*,\)\([^,]\+\),*/\2/;
 s/^,*$/(empty)/;
'

Upvotes: 0

Daniel
Daniel

Reputation: 4549

sed -e "s/,*\([^,]*\).*/\1/"

should find the first, and

sed -e "s/\(,*\([^,][^,]*\)\)*,*/\2/"

should find the last.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246807

Perl is good for this task

perl -F, -lane '@G = grep {$_ ne ""} @F; print $G[0]' file

The grep command filters out empty fields.
To print the last field, change $G[0] to $G[-1]


With sed:

 sed 's/^,*//; s/,.*//' file   # first field
 sed 's/,*$//; s/.*,//' file   # last field

Upvotes: 1

anubhava
anubhava

Reputation: 785156

Using awk with custom field separator:

First non-empty field:

awk -F ',+' '{print $2}' file
VALUE1
VALUE3

VALUE6
VALUE8

Last non-empty field:

awk -F ',+' '{print $(NF-1)}' file
VALUE2
VALUE5

VALUE7
VALUE8

Regex pattern ',+' will make 1 or more commas as a field separator.

Upvotes: 4

Jotne
Jotne

Reputation: 41456

From this data:

cat file
,,,,,VALUE1,,,,,VALUE2,,,,
,,VALUE3,,,VALUE4,,,VALUE5,
,,,,,,,,,,,,,,,,,,,
,,,,,,,VALUE6,,,,VALUE7,,
,,,,,,,,VALUE8,,,,,,

First occurrence:

tr ',' ' ' <file | awk '{print $1}'
VALUE1
VALUE3

VALUE6
VALUE8

Last:

tr ',' ' ' <file | awk '{print $NF}'
VALUE2
VALUE5

VALUE7
VALUE8

Upvotes: 0

Related Questions