1

I want to modify my tab-delimited file like as below.

Original file

a001.2[tab]xxx[tab]001[tab]zz
bbb[tab]ddd[tab]293[tab]kkjk
c054.9[tab]ccc[tab]786[tab]qq

Modified file

a001[tab]xxx[tab]001[tab]zz
bbb[tab]ddd[tab]293[tab]kkjk
c054[tab]ccc[tab]786[tab]qq

I want to remove decimal (ex. a001.2 to a001) at the first column only using bash scripts.
Please let me know how should I do.
Thank you in advance.

3
  • Do you mean ;s are actually tabs? Commented May 18, 2016 at 1:20
  • Sorry, HTML tag doesn't work. I fixed to [tab]. Thanks for catching that. :-) Commented May 18, 2016 at 1:25
  • Does it have to be a shell construct or you are open to sed, awk etc? Commented May 18, 2016 at 1:26

1 Answer 1

2

With sed:

sed -E 's/^([^\t]+)\.[^\t]+(.*)/\1\2/' file.txt

If the file is not large, you can use bash while loop with parameter expansion:

while IFS=$'\t' read i j; do echo "${i%%.*}"$'\t'"$j"; done <file.txt

Example:

$ cat file.txt  
a001.2  xxx 001 zz
bbb     ddd 293 kkjk
c054.9  ccc 786 qq

$ sed -E 's/^([^\t]+)\.[^\t]+(.*)/\1\2/' file.txt
a001    xxx 001 zz
bbb     ddd 293 kkjk
c054    ccc 786 qq

$ while IFS=$'\t' read i j; do echo "${i%%.*}"$'\t'"$j"; done <file.txt
a001    xxx 001 zz
bbb     ddd 293 kkjk
c054    ccc 786 qq
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.