Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have few files like A.txt, B.txt and C.txt, each having a only single number in it i.e A.txt has 45 B.txt has 35 and C.txt has 100. How could I read these entries from each files and report with its corresponding file name.

output:

45 A.txt

35 B.txt

100 C.txt

thanks!

share|improve this question

3 Answers 3

One way:

awk '{ print $0 " " FILENAME }' A.txt B.txt C.txt

Output:

45 A.txt
35 B.txt
100 C.txt
share|improve this answer

Another answer, which doesn't involve awk but gives you the output in another format:

$ grep '.*' *.txt
A.txt:45
B.txt:35
C.txt:100
share|improve this answer

With shell only:

for f in {A,B,C}.txt; do echo $(<$f) $f; done

Output:

45 A.txt
35 B.txt
100 C.txt
share|improve this answer
    
I think that one's better than mine! –  Jenny D Aug 23 '12 at 14:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.