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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

edit file after it is generated 1

Status
Not open for further replies.

unixwhoopie

Programmer
May 6, 2003
45
US
I have an external system create a file which contains the number of lines in the file. I need to make sure that this number is correct. So as soon as the file is created, I need to count the number of lines in the file and edit it if the information in the file is not correct. Any ideas how to edit a text file after it is generated?

Thanks,
 
man ex

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
how is the file created? I know the external system, but how... what is this external system?
how do you know when the file is created?
how do you know the name?
 
1 - To count the number to lines in a file that is created you can use the wc command.

wc -l (lower case l and not a one) filename


2 - To edit the file you can use the vi editor.

vi filename

If you are not familiar with how to use the vi editor do a search online for vi cheatsheet.


HTH,


Steve
 
Use a text editor.


I assume you mean to edit it from a script, but you should be more clear on that.

In that case, you can write a sed script. Sed takes a file as input, performs some operation on it, and outputs the resulting file.

Awk, while not exactly an editor, might work, too.

You can also use ed. Ed is a primitive editor that also supports scripting, and might be easier to use than sed for some things.

I'm sure other editors have scripting features, as well.
 
Can you give me more details about editing using ed? The number lines is printed in the 2nd line and it is character based, so I exactly have to edit those characters. I would know when the file is created, so it just a matter of calling the script to check and edit.

Thanks
 
Just a word of caution.

Are you certain that nothing will modify the file after it has been created?

The reason for the question is UNIX does not keep creation dates for files but tracks the last modification date instead.

And while AWK is not an editor it can and does allow subtitution and editing of data. It is also more powerful than SED in that it allows you to use programmatic logic as well.

HTH,

Steve
 
The number lines is printed in the 2nd line (...) the script to check and edit.
Something like this ?
Code:
awk '{a[NR]=$0}
END{a[2]=NR;for(i=1;i<=NR;++i)print a[i]}
' </path/to/input >output && mv output /path/to/input
Another way, in ksh, with wc and ed:
Code:
typeset -i n=$(wc -l</path/to/file)
echo "2c\n$n\n.\nwq" | ed -s /path/to/file


Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top