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
  • 56,709
  • 26
  • 150
  • 232
M.Nehru
  • 91

2 Answers2

0
Length=$(wc -l < Final.txt)
echo $Length
sed -i "1 i\<records>$Length</records>" Final.txt
Kamaraj
  • 4,365
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,078