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

Creating a Text file and counting Lines

Status
Not open for further replies.

akki007

Programmer
May 22, 2003
55
GB
I am trying to carry out the following process from within a unix script....

1. Create a new Text file containing a heading (Text.txt)
2. Count the number of rows in a seperate file (File.dat)
3. Store this figure in a variable
4. Insert this figure into the previously created text file
5. Save the text file

Any thoughts

Thanks
 
what are your thoughts/ideas so far?


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I have no clue, but I would expect to use....

awk 'END { print NR }' file.txt

Somewhere along the way!
 
how about:

#!/bin/ksh

typeset -l rows=$(wc -l < File.dat)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
As you have probably guessed, I have very little UNIX experience, please could you explain what this will do? I am guessing that it will put the count of the number of rows into a variable 'rows'?

How do I create a blank text file and insert the value in this variable into the file?
 
#!/bin/ksh

myBlankFile='Text.txt'
typeset -l rows=$(wc -l < File.dat)

echo &quot;${rows}&quot; > &quot;${myBlankFile}&quot;

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Ok, I think I understand that. One final question though...

How do I insert a header record that reads....

&quot;The number of rows in File.dat = &quot; rows
 
Write that string (&quot;.. number of rows ..&quot;) to another file - temp1.txt say, rename your data file to a temp name, temp2.txt say, and then use cat to concatenate two files into one, final, output file.

cat temp1.txt temp2.txt > File.dat


Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Can I not use the same principle as the row count?

header=&quot;Number of rows in file = &quot;
echo &quot;${header}&quot; > &quot;${{myBlankFile}&quot;
 
Hi,

Using > will overwrite the file and >> will append to the end, wheras you want to insert to the beginning.

Using cat as Mike suggested will append the 2 files together and output the results to a new file.

You could also use sed to insert to the first line.

sed '1iNumber of rows' temp2.txt > temp3.txt

Matt.
 
Ok, so if I understand correctly, could I do this....

#!/bin/ksh
myBlankFile='Text.txt'
header=&quot;Number of rows in file = &quot;
echo &quot;${header}&quot; > &quot;${myBlankFile}&quot;
typeset -l rows=$(wc -l < File.dat)
echo &quot;${rows}&quot; >> &quot;${myBlankFile}&quot;

The file would then appear as....

<b>Number of rows in file = 150</b>
 
Ok, so if I understand correctly, could I do this....

#!/bin/ksh
myBlankFile='Text.txt'
header=&quot;Number of rows in file = &quot;
echo &quot;${header}&quot; > &quot;${myBlankFile}&quot;
typeset -l rows=$(wc -l < File.dat)
echo &quot;${rows}&quot; >> &quot;${myBlankFile}&quot;

The file would then appear as....

Number of rows in file = 150
 
#!/bin/ksh
myBlankFile='Text.txt'
header=&quot;Number of rows in file = &quot;
typeset -l rows=$(wc -l < File.dat)
echo &quot;${header} ${rows}&quot; > &quot;${myBlankFile}&quot;


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
That sounds like a winner! Thanks. Ive learnt a lot! One final thing (honestly!)

If I wanted to insert a header and then insert a blank line, how would I do that? Eg.

TITLE OF FILE<<char return>>
=============<<char return>>
<<char return>>
Total number of rows in file = 150<<char return>>
 
No,
you would instead get

Number of rows in file =
150

because by default echo will insert a new line.

Try ...

#!/bin/ksh
myBlankFile='Text.txt'
header=&quot;Number of rows in file = &quot;
typeset -l rows=$(wc -l < File.dat)
echo &quot;${header}${rows}&quot; > &quot;${myBlankFile}&quot;

if you wanted to stop echo inserting a new line use \c

e.g.

echo &quot;Hello \c&quot; > blah.txt
echo &quot;there&quot; >> blah.txt

cat blah.txt would return ...

Hello there.

Matt.
 
Sorry,

I'm a bit of a slow typer ;-)

use \n in the echo to insert an extra carriage return

Matt.
 
#!/bin/ksh
myBlankFile='Text.txt'
title='TITLE OF FILE'
header=&quot;Number of rows in file = &quot;
typeset -l rows=$(wc -l < File.dat)

cat > &quot;${myBlankFile}&quot; <<EOF
${title}
=============

${header} ${rows}
EOF


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Sorry, the last post went so far above my head I could hardly see it. Can anyone explain it please?
 
It's saying cat everyting between <<EOF and EOF out to myblankfile and using the variables you assigned previously so you would get a file containing ..

TITLE OF FILE
=============

Total number of rows in file = 150
 
I see. Thankyou for your help, this should give me exactly what I need. Also thanks for the introduction to UNIX.

Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top