Operating System - HP-UX
1844137 Members
3758 Online
110228 Solutions
New Discussion

problem with shell script - reading folder

 
SOLVED
Go to solution
amonamon
Regular Advisor

problem with shell script - reading folder

Hello..

I have a strange problem with my shell script..I have in folder meny txt and doc files I am only interested in txt files..how can I read certan data from each file and write it back to one file..

any ideas?

I have like 300 txt files in one folder and there is in each file peace of info that I need each file has this structure:

mysql -u -h -p
THIS LINE IS TARGET

so basicly I only need last line from txt file..tail -1 is target value..

any ideas or start code??
13 REPLIES 13
Marvin Strong
Honored Contributor

Re: problem with shell script - reading folder

for a in *.txt
do
tail -1 $a >> newfile
done
Ninad_1
Honored Contributor
Solution

Re: problem with shell script - reading folder

If you mean by txt and doc files as .txt and .doc files, then you can use the following

for i in `ls -1 *.txt`
do
tail -1 $i >> ouputfile
done

Regards,
Ninad
spex
Honored Contributor

Re: problem with shell script - reading folder

find . -type f -name '*.txt' -exec tail -1 {} >> accum.txt \;
Enrico P.
Honored Contributor

Re: problem with shell script - reading folder

Hi,

for FILE in `ls`; do
if tail -1 $FILE|grep ^mysql >> /dev/null; then
tail -1 $FILE >> ouputfile
fi
done

Enrico
amonamon
Regular Advisor

Re: problem with shell script - reading folder

thanks you guys gave me a good hint...thanks for code..
OldSchool
Honored Contributor

Re: problem with shell script - reading folder

If the lines of interest are always in .txt files contained in one directory (/folder for example), and the line that you are interested in is always "mysql -u", then the following should work just fine:

grep 'mysql -u' *.txt > accum.txt

amonamon
Regular Advisor

Re: problem with shell script - reading folder

thanks but tail has different values not always same..

I figure it out..but now I have anothe small problem..

I have this:
awk '{printf("INSERT INTO akcije (date, time, target1, target, action) VALUES ('%s','%s','%s','%s','%s');\n",substr($0,0,6),substr($0,8,17),substr($0,25,8),substr($0,33,6),substr($0,48,11));}' ca > CAndA.sq

result is: from CAndA.sq file:
..
...
INSERT INTO akcije (date, time, target1, target, action) VALUES (20060524,145222,5431,876,west);
...
.

which is not good command for mysql it needs to be like this:

INSERT INTO akcije (date, time, target1, target, action) VALUES ('20060524','145222','5431','876','west');

how can I show those ''...
I also tryed with \' insted just ' but same result..any ideas?

Thanks..
Enrico P.
Honored Contributor

Re: problem with shell script - reading folder

Hi,
try

awk '{printf("INSERT INTO akcije (date, time, target1, target, action) VALUES ('%s','%s','%s','%s','%s');\n",substr($0,0,6),substr($0,8,17),substr($0,25,8),substr($0,3
3,6),substr($0,48,11));}' ca |sed s/\'//g > CAndA.sq

Enrico
amonamon
Regular Advisor

Re: problem with shell script - reading folder

thanks for help but I get a same result with sed command...

same with this sed as without sed..
Enrico P.
Honored Contributor

Re: problem with shell script - reading folder

Sorry,
try to put the

"'"'"'"

before and after the substr function:

awk '{printf("INSERT INTO akcije (date, time, target1, target, action) VALUES ('%s','%s','%s','%s','%s');\n","'"'"'"substr($0,0,6)"'"'"'","'"'"'"substr($0,8,17)"'"'"'","'"'"'"substr($0,25,8)"'"'"'","'"'"'"substr($0,3
3,6)"'"'"'","'"'"'"substr($0,48,11)"'"'"'");}' ca > CAndA.sq

Enrico
Enrico P.
Honored Contributor

Re: problem with shell script - reading folder

Hi,
in my test it work:

awk '{print "'"'"'"substr($2,2,5)"'"'"'","'"'"'"substr($2,2,5)"'"'"'"}' test

'prova' 'prova'

Enrico
amonamon
Regular Advisor

Re: problem with shell script - reading folder

Enrico...this works perfect...thanks guys for this help..

Cheers,
Enrico P.
Honored Contributor

Re: problem with shell script - reading folder

Hi,

http://www.gnu.org/software/gawk/manual/html_node/Quoting.html

Here there is many example more readable than that I gave you for single quote in awk.

Enrico