Find string in a file and append character

Hi Experts,

Is there a way to find a string in a file then append a character to that string then save the file or save to another file.

Here is an example.

>cat test.txt
NULL
NULL
NULL
9,800.00
NULL
1,234,567.01

I want to find all NON NULL String and add a dollar sign to those numbers.

Output will be..

>cat test.txt
NULL
NULL
NULL
$9,800.00
NULL
$1,234,567.01

*The actual file will contain a lot of numbers.

Thanks in advance.

You can do this with sed:

sed '/^NULL$/!s/^/$/' test.txt > test_fixed.txt

Thank you sooo much chubler!

awk '(!/^NULL/){$1="$"$1}{print}' 1

---------- Post updated at 11:00 AM ---------- Previous update was at 11:00 AM ----------

awk '(!/^NULL/){$1="$"$1}{print}' test.txt

Assuming all are numbers except NULL..

sed 's/^[0-9]/$1/g' file

I'm sure pamu had sth like this in mind:

sed 's/^\([0-9]\)/$\1/'
  • otherwise 8000 dollars are gone!
$ awk '/^[0-9]/{$0="$"$0}1' input.txt
NULL
NULL
NULL
$9,800.00
NULL
$1,234,567.01

I am getting below output....

$ cat temp_file
NULL
NULL
NULL
9,800.00
NULL
1,234,567.01

$ sed 's/^[0-9]/$1/g' temp_file
NULL
NULL
NULL
$1,800.00
NULL
$1,234,567.01

$ sed 's/^\([0-9]\)/$\1/' temp_file
NULL
NULL
NULL
$9,800.00
NULL
$1,234,567.01

Yes! look at line #4!

1 Like

Thanks Rudic...:slight_smile: