Replacing a field in pipe delimited TEXT File


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing a field in pipe delimited TEXT File
# 1  
Old 01-06-2009
Replacing a field in pipe delimited TEXT File

Hi,
I want to replace a field in a text delimited file with the actual number of records in the same file.

Code:
HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ
DTL|0|5464-1|0|02-02-2008|02-03-2008||||F|||||||||
DTL|1|5464-1|1|02-02-2008|02-03-2008|1||JJJ
DTL|2|5464-1|2|02-02-2008|02-03-2008|1||JJJ
DTL|3|5464-1|3|02-02-2008|02-03-2008|1||JJJ
DTL|4|5464-1|4|02-02-2008|02-03-2008|1||JJJ
DTL|0|9999-1|0|02-02-2008|02-03-2008||||F|||||||||
DTL|1|9999-1|1|02-02-2008|02-03-2008|1||zzz
DTL|2|9999-1|2|02-02-2008|02-03-2008|1||zzz
TRL|ABCD|10-13-2008 to 10-19-2008.Txt|10-19-2008|170|XYZ

I would like to replace "170" in the Tail/Last record with (8) number of records with "DTL" in the same file( 8 in the above example)...is it possible to work and write on the same file ...rather than following way where i need to O/P it to a new file and then rename the temp file with the original file?

Code:
nawk 'BEGIN{OFS=FS="|"} $1=="TRL" {$5='8'} {print}' null.txt > temporary.txt
 
nawk 'BEGIN{OFS=FS="|"} $1=="TRL" {$5='$count'} {print}' null.txt > temporary.txt


Can i work and write the output i need on the same file assuming that i have the records count stored in a seperate variable.Any suggestion wud be appreciated.


thanks,
ravi
# 2  
Old 01-08-2009
No, you have to do it using a temporary file.

Some utilities, such as perl and GNU sed have a -i or "in-place" option to do that... but they are really doing exactly the same thing and just automating it for you.
# 3  
Old 01-08-2009
It is possible in perl to read a file into an array (memory structure), do things to the array, then output the array into a file, possibly overwriting the same file. This is probably harder than simply making a temp file.
# 4  
Old 01-08-2009
MySQL

Thanks guys...

I just stuck to the solution i mentioned....outputting it to a new temporary file...its easier that way.


thanks,
R
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

3. Shell Programming and Scripting

Replacing a column in a pipe delimited file

Hi, I have a pipe delimited file as below and I need to replace the 2nd column of each line with null values. 1|10/15/2011|fname1|lname1 2|10/15/2012|fname2|lname2 3|10/15/2013|fname3|lname3 Output file: 1||fname1|lname1 2||fname2|lname2 3||fname3|lname3 I tried this ... (2 Replies)
Discussion started by: member2014
2 Replies

4. Shell Programming and Scripting

Pivoting csv field from pipe delimited file

Hello All, Thanks for taking time to read through the thread and for providing any possible solution. I am trying to pivot a comma separated field in a pipe delimited file. Data looks something like this: Field1|Field2 123|345,567,789 234|563,560 345|975,098,985,397,984 456|736 Desired... (8 Replies)
Discussion started by: svks1985
8 Replies

5. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use cat file | sed 's/|//t/g' The above command substituted "/t" not tab in the place of pipe. Sample file: abc|123|2012-01-30|2012-04-28|xyz have to convert to: abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies

6. UNIX for Dummies Questions & Answers

add new 'date field' in a pipe delimited file

i need to add a new field in a pipe delimited line. the field will be the current date today. aa|a|s|w|1 as|oiy|oiy|oiy|2 given that all lines are uniformed in the number of fields i want it to look like this:\ aa|a|s|w|1|20120126 as|oiy|oiy|oiy|2|20120126 please help :) (3 Replies)
Discussion started by: kokoro
3 Replies

7. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

8. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

9. Shell Programming and Scripting

replace 3rd field of space delimited text file

how to replace the 3rd colum? Each line begins similarly, but they all ends variously. XX YY 03 variable text here XX YY 03 more variable text here XX YY 03 even more variable text here really long setence XX YY 03 variable numbers also appear 03 11. 123 456 XX YY 03 the occasional comma,... (4 Replies)
Discussion started by: ajp7701
4 Replies

10. UNIX for Dummies Questions & Answers

Problem working with Pipe Delimited Text file

Hello all: I have a following textfile data with name inst1.txt HDR|ABCD|10-13-2008 to 10-19-2008.txt|10-19-2008|XYZ DTL|H|5464-1|0|02-02-2008|02-03-2008||||F||||||||| DTL|D|5464-1|1|02-02-2008|02-03-2008|1||JJJ DTL|D|5464-1|2|02-02-2008|02-03-2008|1||JJJ... (9 Replies)
Discussion started by: ravi0435
9 Replies
Login or Register to Ask a Question