display files name and it's content


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers display files name and it's content
# 1  
Old 01-08-2009
Data display files name and it's content

hello,

i have a question and i'll be appreciated if you can help me with this.

i have i folder let's say "users"
in this folder there are files (file1, file2, file2, etc) and in each file there is a number (lets say 4 in file1)

i want to display some thing like bellow

file1 4
file2 3
file3 2
file4 1

i want also the files names to be sorted alphapitical order..

Is this possible ! Smilie (using shell script)
Thanks a lot.
# 2  
Old 01-08-2009
Assuming files in current directory and each file consists of the number as in question:

nawk ' { print FILENAME " " $1 } ' `ls` | sort

gives:

file1 4
file2 3
file3 2
file4 1
# 3  
Old 01-08-2009
thanks for your replay pmm but what if i want to specify the directory path !

regards
# 4  
Old 01-09-2009
The ls command can be used to retrieve any files required from any directory:

nawk ' { print FILENAME " " $1 } ' `ls files/file*` | sort
# 5  
Old 01-09-2009
wow
Thanks a lot pmm, this was very helpfull... i got the bellow

myfiles/file1 4
myfiles/file2 3
myfiles/file3 2
...

can i display this result with any kind of format! like

file name value
file1 4
file2 3
file3 2

and so on

Regards
# 6  
Old 01-11-2009
Ok - try this

nawk ' { print FILENAME "/" $1 }' `ls files/file*` | sort | nawk -F/ '
BEGIN { printf "%10s\t%10s\n","Filename","Value" }
{ printf "%10s\t%10s\n",$(NF-1),$NF } '

First nawk does as previous but makes output:
myfiles/file1/1
etc.
Second nawk does formatting with prints, printing header in BEGIN section, then format printing second last and last fields.
# 7  
Old 01-11-2009
Smilie

thanks a lot pmm

this realy helpfull... i spent hours to find a way to do that.... i really appreciate your help Smilie

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Need Suggestion, on how to constantly moniter a file and display content with in that time

Dear Gurus, I'm in a strange situation, hence need some kind of advice or possible syntax to carry on. let's say current time/date is 2013-12-18, 15:58:15 I got a file something like this, which keep getting updated with respect to time. 2013-12-18,... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

3. Shell Programming and Scripting

Grep to display file name along with content in Solaris

Am using the following grep to match a particular patter in grep. grep xyz abc.txt now while i run this command, if the pattern matched, am getting the line containing xyz Output: xyz is doing some work Now if i want the file name also along with my output, what should i do Expected... (2 Replies)
Discussion started by: rituparna_gupta
2 Replies

4. Shell Programming and Scripting

Display specific lines content from the file

Hell, I want to grep certain word from file and display above 2 lines and after two lines. Here is the content of sample file. Mar 14, 2013 12:56:59 AM Agent.Agent SendTo INFO: Connection to server:7041 - Credential Transmit Successesful Mar 14, 2013 8:54:21 AM cgent SendTo WARNING:... (7 Replies)
Discussion started by: balareddy
7 Replies

5. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

6. UNIX for Dummies Questions & Answers

Display the content of the unix file

cat is the normal unix command to display the content of a file. Is there any program to display the content of the file. If there is, then can you send me the code. (5 Replies)
Discussion started by: vedanjalig
5 Replies

7. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

8. Shell Programming and Scripting

Display the content in different lines in email

Hi, I am checking if a file exists or not and based on the result I am sending an email. The email part works fine but the content of the email comes in a single line and I am not able to display it in diferent lines. if ; then echo "Hi, \\n An exception exists in the process.... (3 Replies)
Discussion started by: shanth_chandra
3 Replies

9. Shell Programming and Scripting

display content between all similar tags pattern

hi, I m stuck at a point for more than 3days. My doubt is pretty simple.. I have a web page content in $content. ( got this by using LWP).. Now I want to display the content matching a pattern. I tried if($content =~ m{<div class="abc">(.*?)</div>}s){ print $1;} that will... (4 Replies)
Discussion started by: therockravi
4 Replies

10. UNIX for Dummies Questions & Answers

grep/cat/more -- search in a txt file and display content from a specific "keyword"

Hi, I have a .txt file Sample: ===================== NEXT HOST ===================== AEADBAS001 ip access-list extended BLA_Incoming_Filter ip access-list extended BLA_Outgoing_Filter access-list 1 permit xxxxxxxxxxxxxx access-list 2 permit xxxxxxxxxxxxxx =====================... (4 Replies)
Discussion started by: I-1
4 Replies
Login or Register to Ask a Question