-1

I have a file FILE1.TXT. It contains only one file name FILE2.TXT.

How will I find the record count / line count of FILE2.TXT using only FILE1.TXT? What I have already tried is:

cat FILE1.TXT | wc -l

But the above command did not work.


Actually, I need to display the output as below:

File name is FILE2.TXT and the count is 2.

What I have already tried is (using the below statement inside a script file):

echo "File name is "`cat FILE1.TXT`" and the count is " `wc -l < $(cat FILE1.TXT)`

But the above command did not work and gave error

syntax error at line 1: `(' unexpected
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
TechVolcano
  • 221
  • 1
  • 3
  • 13
  • You need [Command Substitution](http://www.gnu.org/software/bash/manual/bash.html#Command-Substitution), and you don't need `cat`. – Jonathan Leffler Feb 11 '15 at 04:04
  • possible duplicate of [Looping through the content of a file in Bash?](http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash) – tripleee Feb 11 '15 at 04:08
  • 1
    Your amended command worked for me (using Bash 3.2 on Mac OS X 10.10.2 Yosemite). Which shell are you using? Unless you are using an archaic shell (`/bin/sh` on Solaris, for example), it should work. It might be better to write: `echo "File name is $(cat FILE1.TXT) and the count is " $(wc -l < $(cat FILE1.TXT))` where the ease of nesting is one of the merits of `$(…)` over back quotes. Leaving the `wc` outside the double quotes means that extra white space in the output of `wc` is stripped by `echo`. – Jonathan Leffler Feb 11 '15 at 05:27
  • @JonathanLeffler: Your help is highly appreaciated.... When I change the shebang in the script it worked.. Thanks you so much – TechVolcano Feb 11 '15 at 09:39

2 Answers2

2

For a POSIX-compliant shell:

wc -l $(cat FILE1.txt)

or, with Bash:

wc -l $(<FILE1.txt)

These will both report the file name (but will work if there are multiple file names in FILE1.txt). If you don't want the file name reported (but there's only one name in the file), you could use:

wc -l < $(cat FILE1.txt)
wc -l < $(<FILE1.txt)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
-1
file=$(cat FILE1.txt | grep -o "FILE2.txt")
cat "$file" | wc -l
Shadey
  • 1
  • 2
  • Thanks for the answer. FILE1.TXT conatins only one file name. Any solution without looping will be good. – TechVolcano Feb 11 '15 at 04:26
  • There's no need to use a variable like that. – Jonathan Leffler Feb 11 '15 at 04:40
  • @JonathanLeffler Okay then, what should I do instead? – Shadey Feb 11 '15 at 04:42
  • @JonathanLeffler That doesn't answer the question. Your answer just cats out FILE1 or assumes FILE1.txt only contains FILE2.txt which may or not be true. – Shadey Feb 11 '15 at 04:47
  • i am using an echo to redirect the result as below. echo "File name is " `cat FILE1.txt` " and the count is " and the result should be File name is FILE2.TXT and the count is 2. – TechVolcano Feb 11 '15 at 04:48
  • Try it. `echo FILE2.txt > FILE1.txt; printf '%s\n' a b c d e f g > FILE2.txt; wc -l < $(cat FILE1.txt)` should produce the answer `7`. It does for me. – Jonathan Leffler Feb 11 '15 at 04:49
  • Note that the OP has said that `FILE1.txt` only contains one file name, so we don't have to worry about that (though I did cover the issue in my answer — at least, pointing out that the first variants will work with multiple files whereas the redirection variants will not work properly with multiple files). If you know that the name you're after is FILE2.txt (rather than whatever name happens to be in FILE1.txt, which happens to be FILE2.txt), then you simply write the file name on the command line, rather than grepping it out of FILE1.txt, etc. – Jonathan Leffler Feb 11 '15 at 04:58
  • @user3611976: If you have a specific format for the output, you should identify that output format in the question, not in comments to an answer. You can update your question — but next time, ask the complete question up front and do not ask it incrementally. That annoys people trying to help you. – Jonathan Leffler Feb 11 '15 at 05:00