Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to alter AWK source/input file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
The man page for awk clearly states that awk can be used to alter the contents of the source/input file. But how is this done??? It implies that it does not mean you need to pipe/re-direct the output to the input file.

Thanks.
 
Hi,

During the execution of an END action, the variable FILENAME contain the name of the last input file processed.
You can use it to modify the file like this :
awk 'END{ print "END OF FILE" > FILENAME}' file

Regards.

Fcail
 
.
I didn't get that from the man page.
Like any system command if you want the output saved you
can write to file or to memory.

op=$(awk ' {
if ($0 ~ /bogusstring/) {
printf "%s", substr(1,1,$0)
}' targetfile) ; test -z "$op" && echo "Nochar."

or redirect to file in or out of the awk program:

awk ' /[A-Z]/ {
print $0
}' targetfile > holdfile
-----------------------------
awk ' /[0-9]\{2,4\}/ {
printf "%d", $0 >> "/home/me/badnumpats"
}' target

Bye.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top