# How to remove a string from a specific column in a file

**URL:** <https://community.unix.com/t/how-to-remove-a-string-from-a-specific-column-in-a-file/263076>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [April 2, 2010, 10:30am UTC](https://community.unix.com/t/how-to-remove-a-string-from-a-specific-column-in-a-file/263076 "2010-04-02T10:30:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![auburn](https://community.unix.com/letter_avatar/auburn/32/5_5575768a8748004e209b776fc1b2916d.png) [@auburn](https://community.unix.com/u/auburn)\
**Post date:** [April 2, 2010, 10:30am UTC](https://community.unix.com/t/how-to-remove-a-string-from-a-specific-column-in-a-file/263076/1 "2010-04-02T10:30:06Z")

</div>

Hello,  
A basic query. How can I remove a string from a specific column.  
For example, remove "abcd" just from column 2 in example file:

abcd abcd1  
abcd abcd2  
abcd abcd3

to get output:

abcd 1  
abcd 2  
abcd 3

Thank you!🙂

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/5_5575768a8748004e209b776fc1b2916d.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [April 2, 2010, 10:52am UTC](https://community.unix.com/t/how-to-remove-a-string-from-a-specific-column-in-a-file/263076/2 "2010-04-02T10:52:04Z")

</div>

One way:

```nohighlight
 awk '{ gsub(/abcd/,"", $2); print } ' filename > newfilename

```

---

<div class="post-metadata">

**Author:** ![in2nix4life](https://community.unix.com/user_avatar/community.unix.com/in2nix4life/32/709_2.png) [@in2nix4life](https://community.unix.com/u/in2nix4life)\
**Post date:** [April 2, 2010, 10:54am UTC](https://community.unix.com/t/how-to-remove-a-string-from-a-specific-column-in-a-file/263076/3 "2010-04-02T10:54:16Z")

</div>

Using Perl one-liner:

perl -pe 's/^([a-z]+).\*([0-9]+)/$1 $2/g' file

abcd 1  
abcd 2  
abcd 3

Too make change to file permanent, add the -i + extension switch to make a backup of the file, then make change to original file.

perl -i.bak -pe 's/^([a-z]+).\*([0-9]+)/$1 $2/g' file

Should end up with "file" and "file.bak" which is a backup of the original file. Original "file" now contains:

abcd 1  
abcd 2  
abcd 3

---

<div class="post-metadata">

**Author:** ![auburn](https://community.unix.com/letter_avatar/auburn/32/5_5575768a8748004e209b776fc1b2916d.png) [@auburn](https://community.unix.com/u/auburn)\
**Post date:** [April 2, 2010, 11:04am UTC](https://community.unix.com/t/how-to-remove-a-string-from-a-specific-column-in-a-file/263076/4 "2010-04-02T11:04:54Z")

</div>

Thanks everyone!

---

<div class="post-metadata">

**Author:** ![sdanner](https://community.unix.com/letter_avatar/sdanner/32/5_5575768a8748004e209b776fc1b2916d.png) [@sdanner](https://community.unix.com/u/sdanner)\
**Post date:** [April 5, 2010, 12:56pm UTC](https://community.unix.com/t/how-to-remove-a-string-from-a-specific-column-in-a-file/263076/5 "2010-04-05T12:56:24Z")

</div>

cut -c1-5,10- filename

I did this with the following file:  
abcd abcd1  
abcd abcd14  
abcd abcd127

and got  
abcd 1  
abcd 14  
abcd 127
