0
Length=$(cat Final.txt|wc -l)
echo $Length
sed -i '1 i\<records>$(echo $Length)</records>' Final.txt

I need to the add the line XXX where XXX should be the variable which represents the no.of lines.I have tried the above code and other combinations.None of them seems to work.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
M.Nehru
  • 91
  • 2
  • 7
  • single quotes don't expand anything inside them, double quotes do. Compare `echo '$Length'` and `echo "$Length"`. (Besides, the command substitution and `echo` in the `sed` command are unnecessary). shellcheck.net would actually tell you both of those. – ilkkachu Mar 08 '18 at 09:23
  • 1
    references: [What's a good mnemonic for shell double vs. single quotes?](https://unix.stackexchange.com/q/400447/170373) and http://mywiki.wooledge.org/Quotes – ilkkachu Mar 08 '18 at 09:25

2 Answers2

0
Length=$(wc -l < Final.txt)
echo $Length
sed -i "1 i\<records>$Length</records>" Final.txt
Kamaraj
  • 4,295
  • 1
  • 12
  • 18
0
sed -i "1 i\<records>$(wc -l Final.txt < Final.txt)</records>" Final.txt
  1. You don't need an intermediate variable.

  2. Edit: Per user kamaraj & per user DonChrissti's commened, removed claim that you need the cut command, or some other way to parse out the file name which wc outputs along with the number of lines. A simple I/O redirection hides that from wc, so without knowing what file is being processed, it has nothing to print.

user1404316
  • 3,028
  • 12
  • 23