paulie.jvenuez
paulie.jvenuez

Reputation: 295

sed remove and replace certain match

I have the following in a file.

A 01/13/13 \\0101 \\0102 \\0103
C 04/19/13 \\0301 \\0302 \\0303 \\0304 \\0305
F 04/05/13 \\0602 \\0603 \\0604

And i want to replace the first \\ with the letter at the beginning of the line, and an underscore. Its always one letter. And remove everything afterwards. There is only one space between each section of the lines if that helps.

The desired outcome should be

A 01/13/13 A_0101
C 04/19/13 C_0301
F 04/05/13 F_0602

I tried using grep, how can i do this using sed?

Upvotes: 1

Views: 112

Answers (3)

Jotne
Jotne

Reputation: 41460

You can also use awk like this

awk '{sub(/\\\\/,x);print $1,$2,$1"_"$3}' file
A 01/13/13 A_0101
C 04/19/13 C_0301
F 04/05/13 F_0602

Upvotes: 2

hwnd
hwnd

Reputation: 70750

One way to do this is to enable extended regular expressions by passing the -r flag.

sed -re 's/^(.) (\S+) \\\\(\S+).*$/\1 \2 \1_\3/' file

Output

A 01/13/13 A_0101
C 04/19/13 C_0301
F 04/05/13 F_0602

Upvotes: 3

iruvar
iruvar

Reputation: 23394

is probably better suited to handling this

awk '{sub(/../, $1"_", $3); print($1, $2, $3)}' file.txt
A 01/13/13 A_0101
C 04/19/13 C_0301
F 04/05/13 F_0602

Upvotes: 2

Related Questions