Replacing File values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing File values
# 1  
Old 05-29-2009
Replacing File values

Currently I am using the tr command in 3 scenarios for a ksh script.

1) Replacing any spaces in the file with a ~

tr ' ' '~' <$orignalFile> $newFile

2) After certain processing is done at the end of the scirpt i convert the Tilde back to spaces

tr ' ' '~' <$newFile> $newFile2

3) Last I need to remove any carriage return from the file.

tr -d '\r' <$newFile2> $newFile3

Currently the script works however I am left with 2 extra files that I delete within the script. Is there a more effective way of acheiving what I listed above without having to create 3 new files, I guess I'm suprised when issuing the TR command it doesn't allow you to copy to the same file which i have tried mutlpie times without success ie..

tr ' ' '~' <$orignalFile> $originalFile

Thanks
# 2  
Old 05-30-2009
if you have higher version of sed it comes with an option
sed -i (edit files in place) you can use that to replace space , "~" or anything else without making any temp files
# 3  
Old 05-30-2009
You can't use the same file for input and output as redirection . You will always need to use a temp file and then move it later.

Code:
tr ' ' '~' <newfile > new.tmp ;; mv new.tmp newfile

For your first question, it depends on what processing you will do it to the file after converting spaces to a ~ and then when/why do you revert back.


-Devaraj Takhellambam
# 4  
Old 05-31-2009
Quote:
For your first question, it depends on what processing you will do it to the file after converting spaces to a ~ and then when/why do you revert back.
I am working with a csv. which I am building a specified table for another applicaiton.

data1,This has a space
data2, This has a space
data3, This has a space

I am using AWK to split on the space to seperate the two columns however I noticed when I was looping through csv file the loop was seperating each value instead of keeping data1 and This has a space as two elements. I noticed if I replaced the space with a ~ i would then keep the two value relationship while looping. ie.

data1
This~has~a~space

instead of
data1
This
has
a
space

I tried changing the IFS to IFS=, however that was causing an issue for me for the back end processing of the table of keeping track via a counter of when to add a specified # for the table format between every 2 values and then no # at the end of the file. The other issue i noticed was a carriage return at the end of each 2nd value in the csv value which is where I used the TR -d to remove it, which worked.

I am achieving the end result however I'm always looking for a more efficient way. I then would strip the ~ back out and return spaces.

data1
This has a space
#
data2
This has a space
#
data3
This has a space
<no # at end of file>

I am curious with the SED command is that a per line edit comand or does it operate in the same fashion of the tr command of replacing/removing everything in the file at once?
# 5  
Old 05-31-2009
Code:
awk -F, '{sub(/^ /,"",$2);printf("%s\n%s\n",$1,$2)}' input

HTML Code:
data1
This has a space
data2
This has a space
data3
This has a space

-Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replacing values inside a file.

Good day guys, I'm having trouble in creating a logic when it comes to replacing the values inside a file. I tried using sed command but it just doesn't work the way I want it to be. Here is what I'm trying to achieve. If my input file contains the values below. NAME++GUEST1 ++GUESS2++... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

2. UNIX for Dummies Questions & Answers

Help in replacing column values

Hello All, I am having the file as below .I need to replace column 9-12 with some other values. In the below file I need to replace 1509 to 1508 and 1508 to 1507 .Can you please help me in how to do that Thanks, Arun ... (10 Replies)
Discussion started by: arunkumar_mca
10 Replies

3. Shell Programming and Scripting

Formatting and replacing according to block values

please consider the following file, there are repeated blocks of m values, and nested s values within. there are 2 columns (cols 3 and 4)associated with each m,s combination. All s1 rows must get a value of a(col 3 in output), all s2 values must get a value of b(col 3 in output). If s1 and s2 rows... (1 Reply)
Discussion started by: senhia83
1 Replies

4. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

5. UNIX for Dummies Questions & Answers

Replacing values in a column if they equal a certain value

Hi, I have a tab delimited text file where some lines have the string "NA" in the second column. For these lines, I want to replace NA with the value in the first column, the symbol underscore followed by the value in the fourth column. How do I go about doing that? Thanks! Input: 1 ... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. Shell Programming and Scripting

Replacing column values

hi all , ( perl) i am trying to replace jst one column in a file for eg month dayofweek dealar car-name jan thurs mercedes c300 feb wed lexus is300 all this data is in a master file and i want to replace jan with 1 feb... (5 Replies)
Discussion started by: technoman
5 Replies

7. Shell Programming and Scripting

Replacing string values from a File

I want to replace string values from a file to a file For eg : File1 has 30 lines of string with values, those specific values needs to be changed in file2 and remaining values in file2 should be as it is. For example: From file (File1) cluster.name=secondaryCluster To replace File... (9 Replies)
Discussion started by: sriram003
9 Replies

8. Shell Programming and Scripting

Replacing column 1 in one file with values in other file

Please help me with an shell / awk script to achieve following; File-1: ABCDW01 12322 23322 BDADW01 22232 24453 EDFAW00 32232 23422 and so on, notice that the first coloumn is a code and the another file contains the real value of each entry in the first colum above but not in a... (4 Replies)
Discussion started by: digipak
4 Replies

9. Shell Programming and Scripting

Replacing values in a file based on values in another file

Hi I have 2 files:- 1. List of files which consists of names of some output files. 2. A delimited file; delimted by "|" I want to replace the value of the $23 (23rd column) in the delimited file with name in the first file. It is always position to position. Meaning first row of the first... (5 Replies)
Discussion started by: pparthiv
5 Replies

10. Shell Programming and Scripting

replacing a line that may have different values on different servers

Replace disable_functions in php.ini with value of your choice which may be different on different servers ================================================ -bash-2.05b# grep disable_functions /usr/local/lib/php.ini disable_functions = 1 2 e weq t ret rye y etyhty rt et e ... (3 Replies)
Discussion started by: fed.linuxgossip
3 Replies
Login or Register to Ask a Question