Visit Our UNIX and Linux User Community


Find string in a file and append character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find string in a file and append character
# 1  
Old 08-20-2012
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.
Code:
>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..
Code:
>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.

Last edited by Franklin52; 08-21-2012 at 05:33 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 08-20-2012
You can do this with sed:

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

# 3  
Old 08-21-2012
Thank you sooo much chubler!
# 4  
Old 08-21-2012
Code:
awk '(!/^NULL/){$1="$"$1}{print}' 1

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

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

# 5  
Old 08-21-2012
Assuming all are numbers except NULL..

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

# 6  
Old 08-21-2012
I'm sure pamu had sth like this in mind:
Code:
sed 's/^\([0-9]\)/$\1/'

- otherwise 8000 dollars are gone!
# 7  
Old 08-21-2012
Code:
$ awk '/^[0-9]/{$0="$"$0}1' input.txt
NULL
NULL
NULL
$9,800.00
NULL
$1,234,567.01


Previous Thread | Next Thread
Test Your Knowledge in Computers #477
Difficulty: Medium
The first hard disk (IBM 350) was developed in 1956 by IBM and had a capacity of 3.75MB.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

2. Shell Programming and Scripting

To append Character in file

hi, i am having a file which is comma seperated , i need to append a char in particular column for eg: infile aa,bb,c,dd,ee zz,yy,sss,ddd,ff . . i need to append for eg 4th column with a char like 'LQ' output: aa,bb,c,LQdd,ee zz,yy,sss,LQddd,ff (12 Replies)
Discussion started by: rohit_shinez
12 Replies

3. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

4. Shell Programming and Scripting

Search for a string, then append character to end of that line only

I have 2 files that I am working with $ cat file1 server1 server3 server5 server6 server8 $ cat file2 server1;Solaris; server2; SLES; server3;Linux; server4; Solaris; server5;SLES; server6;SLES; server7;Solaris; server8;Linux; (1 Reply)
Discussion started by: snoman1
1 Replies

5. Shell Programming and Scripting

sed to find first appearance and append string

I have a file like below #GROUP A belongs to Asia GROUP A jojh hans local admin GROUP A gege fans michel jing jong #GROUP U belongs to USA GROUP U jeff goal hello world My requirement is to grep for first apperence of GROUP A which is not commented and append my name to end of file.... (12 Replies)
Discussion started by: vkk
12 Replies

6. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

7. Shell Programming and Scripting

How do I append a ^M to the end of each 129 character string

Hello all, I have a stumper of a problem. I am trying to append a ^M or "newline" to the end of each 129 character string in a huge file in unix. Each string starts with A00. I am trying to get the file to go from... A00vswjdv1 Test Junk Junk A00vswjdv2 Test Junk Junk ... (6 Replies)
Discussion started by: Captain
6 Replies

8. Shell Programming and Scripting

append a character at end of each line of a file

Hi, i want to append a character '|' at end of each line of a file abc.txt. for example if the file abc.txt conatins: a|b|c 1|2|33 w|2|11 i want result file xyz.txt a|b|c| 1|2|33| w|2|11| I know this is simple but sumhow i am not able to reach end of line. its urgent, thanks for... (4 Replies)
Discussion started by: muaz
4 Replies

9. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies

10. Programming

append character to a file

Hi, fputc() will overwrite from the beginning of the target file. How would I append characters to the target file's end instead? Thanks. (3 Replies)
Discussion started by: enuenu
3 Replies

Featured Tech Videos