LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-20-2010, 04:53 PM   #1
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Rep: Reputation: 0
gawk -- print in a new line without overwriting


Hi,
I have a directory where there are folders and in them some .txt data files. I am trying to output a .txt file with the folder name and the number of .txt files in it using gawk. However, when I run my shell program, because print is nested in a "while" loop, gawk overwrites what's already saved in the output file. I want gawk to print "new" output in a new line without overwriting the already existing text in the output file. Any help is much appreciated! Thanks!

#! /bin/sh

# getting the folder list and the number of folders

ls | gawk '{print}' > ../folder_list.txt
i=`cat ../folder_list.txt | wc -l`

# taking the file count in each of the folders
while read line
do
cd $line
filecount=`ls *.txt |wc -l`

# this is where the problem is!
echo "$line" $filecount | gawk '{if (NF !=0) print $1,$2}' > ../out.txt

cd ../
done < ../folder_list.txt
 
Old 09-20-2010, 06:47 PM   #2
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
#!/usr/bin/env ruby 

hash={};hash.default=0
Dir["**/*.txt"].each do |file|
  s=file.split("/")
  hash[s[0..-2].join("/")] += 1
end
hash.each{|x,y| print "#{x}: #{y}\n"}
 
Old 09-20-2010, 06:54 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Well I see heaps of other things that could be fixed up, but in answer to your question:

> means redirect the output to wherever, in your case a file

>> means append to an existing file (and / or create if it doesn't exist)

As you are using >, it clobbers the file with the new input each time the loop hits that line.
 
Old 09-20-2010, 06:54 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Hi, welcome to LQ!

Quote:
Originally Posted by januka View Post
Code:
# this is where the problem is!
  echo "$line" $filecount | gawk '{if (NF !=0) print $1,$2}' > ../out.txt
  
  cd ../
  done < ../folder_list.txt

Simply change
Code:
print $1,$2}' > ../out.txt
to
Code:
print $1,$2}' >> ../out.txt
Potentially rm the out.txt at the beginning of the
scripts so you start anew with each run.



Cheers,
Tink
 
Old 09-21-2010, 08:46 AM   #5
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks

Thanks Kurumi,Grail & Tinkster, didn't realize that it was that simple!
Grail, what are your suggestions?
 
Old 09-21-2010, 09:35 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,562

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Quote:
# getting the folder list and the number of folders
ls | gawk '{print}' > ../folder_list.txt
Well this statement is only true if you can guarantee no files or symbolic links in the directory.

You could just go all bash and get rid of the needles gawks (unless you are trying to learn gawk and then in that case maybe you should try to do it all in gawk).
Something to think about:
Code:
#!/bin/bash

while read -r dir
do
    f_count=0
    for x in $dir/*.txt
    do
        ((f_count++))
    done

    echo "$dir has $f_count txt files" >> out.txt
done< <(find . -maxdepth 1 -type d)
There is a space between the 2 < signs after the last done
 
Old 09-21-2010, 01:30 PM   #7
januka
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks

Great, I'll keep your code in record. Thanks for the help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gawk works from command line but not from cron fantasygoat Linux - Server 3 10-25-2011 01:18 PM
Want to print fouth line after resulted line using grep with tail command saurabhmehan Linux - Newbie 10 08-04-2010 11:38 PM
[SOLVED] specifying fields for printing in gawk from command line David the H. Programming 8 08-04-2009 03:32 PM
writing in terminal: when reaching EOL, no linefeed. Instead overwriting same line SlackerJack Linux - Newbie 2 01-24-2008 11:34 AM
Deleting a line with gawk/awk caps_phisto Linux - General 4 11-06-2004 02:31 PM


All times are GMT -5. The time now is 06:25 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration