Replacing Data in a File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing Data in a File
# 1  
Old 09-18-2012
Replacing Data in a File

Hi All,

Have a requirement where i needs to alter the Content of File once it is generated.

The File consist of Multiple line ...range from 2000-8000 lines.
I need to change the Content of Lines which match this Pattern

1|DT_Buried_Strap_Resistor~RES|DT_Buried_Strap_Resistor~RES|-1.000000e+20|1.000000e+20|-1.000000e+20|1.000000e+20|-1.000000e+20|1.000000e+20|VorR|0|0|

In short it must have 12 | (Pipe) symbols separating data from one another,Must also omit the Lines if it begins with #.

I need to change the value of 12 value to 50.I have highlighted the value in Red.For most of case the value will be 0...i just want to replace it as 50 in every line matching the pattern as i mentioned.

Can anyone help me to achieve this.I dont want to change anything else other than this content and just rewrite the same file in same location as input file passed.

Regards
Ashesh
# 2  
Old 09-18-2012
try this...

Code:
awk -F "|" 'NF == 13 && $0 !~ /^#/ { $12=50}1' OFS=\| file

# 3  
Old 09-18-2012
Hi Pamu,

Seems the script works fine when i run in unix.
But as i am very new to scripting can u please tell me how can i pass the file to this script.

Currently i just replaced the term 'file' in ur script with name of file... But i go back to file and open it remains same as 0...it dosenot write it back to the input file.

---------- Post updated at 12:07 AM ---------- Previous update was at 12:03 AM ----------

Hi Pamu,

I want to pass ther filename as an argument to this script.
# 4  
Old 09-18-2012
Quote:
Originally Posted by asheshrocky
Hi Pamu,

Seems the script works fine when i run in unix.
But as i am very new to scripting can u please tell me how can i pass the file to this script.

Currently i just replaced the term 'file' in ur script with name of file... But i go back to file and open it remains same as 0...it dosenot write it back to the input file.

---------- Post updated at 12:07 AM ---------- Previous update was at 12:03 AM ----------

Hi Pamu,

I want to pass ther filename as an argument to this script.
try this..
Code:
awk -F "|" 'NF == 13 && $0 !~ /^#/ { $12=50}1' OFS=\| file > temp_file #Create one temp file
mv temp_file file   #Transfer it to original file

# 5  
Old 09-18-2012
Hi Pamu,

Thanks for the reply.
Is it possible to Open the InputFile and write back to the same one rather than generating a Temp file and moving it.

---------- Post updated at 01:14 AM ---------- Previous update was at 01:05 AM ----------

Hi,

I pass the value to your script this way.
Code:
<Script id="Dispo">
  <WorkDir>/home/ntp_home/e701152/sapphire/wet/progs/JCalPara</WorkDir>
  <ExecCmd>./test.sh</ExecCmd>
  <TimeOut>600</TimeOut>
   
  <Param id="Dept">${StageWork}/dis/${WaferNC}.limit.dis</Param>
 </Script>

The text displayed in RED is the file that is been passed to Test.sh ( The script ).
I was looking for something which can open this file and rewrite back to it.

Hope u have a clear picture now.

Regards
Ashesh

Last edited by Franklin52; 09-18-2012 at 06:14 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 09-18-2012
Quote:
Originally Posted by asheshrocky
Is it possible to Open the InputFile and write back to the same one rather than generating a Temp file and moving it.
It is always good to avoid performing read and write operation at the same time on a file.

Quote:

<Script id="Dispo">
<WorkDir>/home/ntp_home/e701152/sapphire/wet/progs/JCalPara</WorkDir>
<ExecCmd>./test.sh</ExecCmd>
<TimeOut>600</TimeOut>

<Param id="Dept">${StageWork}/dis/${WaferNC}.limit.dis</Param>
</Script>
To get the value marked in red
Code:
file_name=$(awk -F "[<>]" '/<Param id="Dept">/{print $3}' input_file)

and then perform opertion on the file
Code:
awk -F "|" 'NF == 13 && $0 !~ /^#/ { $12=50}1' OFS=\| $file_name > temp_file 
mv temp_file $file_name

Hope this helps you...Smilie
# 7  
Old 09-18-2012
Hi Pamu,

I still face some issue passing the Input file name to the script.I believe the issue is because its too big.

When i test if the process is running using ps ux i get this result

/bin/bash ./test.sh /home/ntp_home/e701152/sapphire/wet/prodstag/stageWork/FAB8_NoCustomerCode
awk -F | NF == 13 && $0 !~ /^#/ { $12=50}1 OFS=|

The Filename name is Broken after No Customer Code.Actually the Filename runs much longer.

Fullname : FAB8_NoCustomerCode_RIPCORD1.01_8XYJ09015.000_C4-WETILT.01_E-RPCRD1-RERM-01-C4-025-ALL-01_11_EGBNE088TMC5_20120814-232625.limit.wcal.dis
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing 12 columns of one file by second file based on mapping in third file

i have a real data prod file with 80+ fields containing 1k -2k records. i have to extract say 12 columns out of this which are sensitive fields along with one primary key say SEQ_ID (like DOB,account no, name, SEQ_ID, govtid etc) in a lookup file. i have to replace these sensitive fields in... (11 Replies)
Discussion started by: megh12
11 Replies

2. Shell Programming and Scripting

Replace data of a file with data from another file using shell scripting.

Dears, I'm new to shell scripting and i was wondering if you can help me with following matter. I have a file containing 400,000 records. The file contains two columns like: 00611291,0270404000005453 25262597,1580401000016155 25779812,1700403000001786 00388934,1200408000000880... (1 Reply)
Discussion started by: paniklas
1 Replies

3. UNIX for Dummies Questions & Answers

Mapping a data in a file and delete line in source file if data does not exist.

Hi Guys, Please help me with my problem here: I have a source file: 1212 23232 343434 ASAS1 4 3212 23232 343434 ASAS2 4 3234 23232 343434 QWQW1 4 1134 23232 343434 QWQW2 4 3212 23232 343434 QWQW3 4 and a mapping... (4 Replies)
Discussion started by: kokoro
4 Replies

4. Shell Programming and Scripting

Replacing data in one file with data in another

Hello, I have 2 files delimited by "|". File1: 1|New York 12| Buffalo 599| Syracuse File2: 56 Kennedy |1 9 Burridge Pl|15 98 BELL ROCK |599 My goal: Is to replace the numerical numbers in "File 2" (second field, not street address) with the corresponding city names from... (2 Replies)
Discussion started by: vestport
2 Replies

5. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
4 Replies

6. Shell Programming and Scripting

Help in replacing two blank lines with two diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. Sed "/data/{G;G;}/" filename. In the file, after data tag, two lines got inserted blank lines.. Now... (1 Reply)
Discussion started by: arjun_arippa
1 Replies

7. Shell Programming and Scripting

Replacing Comma delimiter coming inside the data.

Hello, I am having flat file (Comma Delimiter) and the data in the file is as given below. EMPNO, ENAME, DESIGNATION, SALARY 10979, Arun Kumar, Cosultant, 35000 13555, Bidhu Shekar, Senior Consultant, 45000 15000, Kiran, Kumar, Senior, Consultant, 40000 If... (9 Replies)
Discussion started by: arunvasu2
9 Replies

8. Shell Programming and Scripting

Find and replace data in text file with data in same file

OK I will do my best to explain what I need help with. I am trying to format an ldif file so I can import it into Oracle oid. I need the file to look like this example. Keep in mind there are 3000 of these in the file. changetype: modify replace: userpassword dn:... (0 Replies)
Discussion started by: timothyha22
0 Replies

9. Shell Programming and Scripting

Replacing data of output file with input

Hi, I have a ksh which peocess and get me data from 3 days... ie if i process it on jan 28.. it gets data for 25, 26 and 27.... the process run every day and get previous 3 days data...all this data is appened to a file lets call time.out Now time.out cannot have deplicate data so what i want... (10 Replies)
Discussion started by: bhagya2340
10 Replies

10. Shell Programming and Scripting

Replacing the last data of each line ina file

Hi, I ahve a file where the format is as given below: qqq dfsf gdfjkg gjdosjg jkflsjd 21 fdksdlf fsdnkfl lkdfsgjld 45 laefsdl fsdlfksl fsdklflk gfsdl 56 I need to replace the last number by space or delete the last number in each line. (by using... (6 Replies)
Discussion started by: jisha
6 Replies
Login or Register to Ask a Question