0

A similar question like this has been asked here, but I would like something slightly different. My issue is this, if my file foobar.txt looks like this:

ABCdefg
hijklmn

How can I search for the ABC on each line of the file, and if "ABC" is found, that entire line needs to be replaced to AAAAAAA. So the final output of the file would be:

AAAAAAA
hijklmn
Community
  • 1
  • 1
John Crawford
  • 9,656
  • 9
  • 31
  • 42

2 Answers2

4

try this sed line:

sed '/ABC/s/.*/AAAAAAA/' file

example:

kent$  echo "ABCdefg
hijklmn
xxABC"|sed '/ABC/s/.*/AAAAAAA/'
AAAAAAA
hijklmn
AAAAAAA

or awk one-liner:

awk '/ABC/{$0="AAAAAAA"}7' file

test omitted.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • 1
    @cnicutar non-zero number, by default awk will do `print $0` – Kent Aug 30 '13 at 22:12
  • I seem to have missed something. My actual file I wanted parsed is here: http://pastebin.com/CRmfLWf8. While my bash file is here: http://pastebin.com/5mYMp2Jr. The file remains unchanged after I run my script file – John Crawford Aug 30 '13 at 22:17
  • 3
    @JohnCrawford if you want to change the file in place, with (gnu)sed you need `sed -i '...' file`, with awk (if your awk < gnu awk 4.1.0): `awk '...' /path/to/file > /tmp/tmpfile && mv /tmp/tmpfile /path/to/file` – Kent Aug 30 '13 at 22:19
  • For completeness sake, maybe also mention `sed '/ABC/c\ AAAAAAA'` – tripleee Aug 31 '13 at 07:09
0

This might work for you (GNU sed):

sed -i '/ABC/s/./A/g' file
potong
  • 55,640
  • 6
  • 51
  • 83