Rui Soares
Rui Soares

Reputation: 21

Linux cut string

In Linux (Cento OS) I have a file that contains a set of additional information that I want to removed. I want to generate a new file with all characters until to the first |.

The file has the following information:

ALFA12345|7890
Beta0-XPTO-2|30452|90 385|29
ZETA2334423 435; 2|2|90dd5|dddd29|dqe3

The output expected will be:

ALFA12345
Beta0 XPTO-2
ZETA2334423 435; 2

That is removed all characters after the character | (inclusive).

Any suggestion for a script that reads File1 and generates File2 with this specific requirement?

Upvotes: 1

Views: 2762

Answers (4)

Chris Seymour
Chris Seymour

Reputation: 85775

And with grep:

$ grep -o '^[^|]*' file1
ALFA12345
Beta0-XPTO-2
ZETA2334423 435; 2

$ grep -o '^[^|]*' file1 > file2

Upvotes: 0

twalberg
twalberg

Reputation: 62369

And, to round out the "big 3", here's the awk version:

awk -F\| '{print $1}' in.dat

Upvotes: 3

crowbent
crowbent

Reputation: 711

You can use a simple sed script.

sed 's/^\([^|]*\).*/\1/g' in.dat 
ALFA12345
Beta0-XPTO-2
ZETA2334423 435; 2

Redirect to a file to capture the output.

sed 's/^\([^|]*\).*/\1/g' in.dat > out.dat

Upvotes: 1

jgr
jgr

Reputation: 3974

Try

cut -d'|' -f1 oldfile > newfile

Upvotes: 5

Related Questions