开发者

Remove strings after each first word in a text file

F开发者_开发问答ile1:

hello   (OPTION1)   123456   123456   123456
world   (OPTION1)   123456   123456   123456
foo     (OPTION1)   123456   123456   123456
bar     (OPTION1)   123456   123456   123456

How would one remove each string after each first word in the textfile File1?

This would probably be down with awk/sed/cat - but I cannot figure it out. I'm still new to editing via these utilities - perhaps others will benefit from my question.

The first words are not predictive (no wild cards), each first word is unique.


awk one liner:

awk '{ print $1 }' < inputfile > outputfile

sed one liner:

sed 's/^\([A-Za-z0-9]*\).*/\1/' < inputfile > outpufile


why to use the those tools even after after having the cut command

cut -f1 -d"\t" filename


try

awk '{print $1}' filename > filename2


You can just trim everything from the first white space onwards:

sed '\s.*$//' <File1


Or just for fun, you could do it with perl

perl -ne '@a=split; print "$a[0]\n"' < file


To remove everything after a delimiter:

cut -f1 -d"\t" inputfile > outputfile

\t can be replaced with any other delimiter (e.g., ;) based on your specific case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜