If I understood you correctly you are trying to take OUTPUT or EDIT the same file which is being used for input as well, then I can say there no option present in awk to do so. You can create a script and execute like following.
Hope this helps.
Could you please try the same and let me know if this helps, kindly try the same first with same details a test file
and if All is Well then you can try with actual file too.
Hope this helps.
Thanks,
R. Singh
Last edited by RavinderSingh13; 02-25-2015 at 05:41 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
This is extremely dangerous code. Depending on the size of the input file and the location of the line being modified in the file, this may truncate your input file to size zero instead of updating a field in one or more lines in the file.
NEVER, try to read and write a file that might be larger than the size of the 1st read performed by awk or you will destroy your input file. (And, since there is no requirement placed on awk for the sizes of the reads it performs, NEVER, NEVER, NEVER do this.)
Using sed -i to update a system configuration file could also leave you with an unbootable system if your system loses power in the middle of an update. Ranvinder's suggestion in post #9 is much safer (using a temp file and moving or copying the updated file after it has been fixed), but it should verify that the awk command returned a zero exit status before attempting the mv on the next line.
However, it would be much better to create the temp file in the same directory as the file being updated (same filesystem is sufficient, but in case more file systems are created in the future, same directory is safer), so the mv is just a rename() which will always leave you with either the original configuration file or the updated configuartion file in place. (Of course, any change to a file could be corrupted by a disk block or controller going bad while you're making the update; but this method eliminates several other error cases that could destroy your data.)
The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format:
input
chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18
chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this?
... (12 Replies)
Hi ,
i am having an file which contains 5 file_name data, i need to read the file name and will perform certain operation and generate out file names with named as 5 individual file_names
for eg:
file.txt contains
file_name1.txt|hai
file_name2.txt|bye
file_name3.txt|how... (3 Replies)
Hi All,
I am hoping someone can help me with some scripting I need to complete using AWK.
I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format.
The Input file is:-
aaaa bbbbb ccccc 1 xxxx aaa bbb
aaaa bbbbb ccccc 2 abcd aaa CCC... (9 Replies)
Hi All,
Sorry if the title is not good but I did not know how to explain with only some words!
What I meant is: I have a unix command built from a private application vendor that when executed it prompts for two entries by the keyboard, let's say, for example:
... (1 Reply)
Hi,
I have a function in a bash script that returns a string after some operations using awk.
The following code returns 555
$VARIABLE="EXAMPLE"
get_number $VARIABLE
this value I'd like to pass it as a second argument of another script with the following usage
myscript.sh <param1>... (7 Replies)
Dear All:
I am trying to do something that (I thought) was relatively straightforward, but my code snippet does not seem to work.
Any suggestions?
Thank you
Sincerely yours
Misha Koshelev
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include... (0 Replies)
Hi,
i am new to awk. I am using csv2pipe script(shown below)
BEGIN { FS=SUBSEP; OFS="|" }
{
result = setcsv($0, ",")
print
}
# setcsv(str, sep) - parse CSV (MS specification) input
# str, the string to be parsed. (Most likely $0.)
# sep, the separator between the values.
#
#... (6 Replies)