ALBI
ALBI

Reputation: 721

Removing a substring from a column in a text file

I have a shell script:

a ab.mi
3 jm.mi
56 uh.mi

I want to remove .mi from the 2nd column and make it like:

a ab
3 jm
56 uh

How can I do this with shell script?

Upvotes: 0

Views: 339

Answers (3)

Purandaran
Purandaran

Reputation: 74

Perl One-Liner:

perl -pe -i.bak 's/\.mi$//i' file

Upvotes: 1

Chris Seymour
Chris Seymour

Reputation: 85883

If you really just want to remove the string .mi then a simple sed substitution will do:

$ sed 's/[.]mi//' file
a ab
3 jm
56 uh

If you really want to restrict to the second column awk is better choice but with sed you could do:

$ sed -r 's/(\S+\s+\S+)[.]mi/\1/' file
a ab
3 jm
56 uh

Upvotes: 1

devnull
devnull

Reputation: 123658

Using awk:

awk 'gsub("\.mi", "", $2)1' inputfile

This would remove .mi from the second column.

Upvotes: 2

Related Questions