is there any shell command to insert a string of characters in every line of a file.. < in the beginning or the end of every line >
views:
44answers:
4Thanks , it worked.. Will I be able to insert line number specific variable in every line < like in line i , can i insert i-1 >
trinity
2010-02-16 17:17:06
You can print the current line number in sed with the '=' command, though it is followed by a \n and I don't recall atm how to get rid of that (short of filtering everything through a second sed, which is ugly). I'll post another answer which tells you how to do what you're asking.
profjim
2010-02-16 17:33:27
This will also work (though results go to stdout rather than to a file).
profjim
2010-02-16 16:57:25
@profjim, no: the `-i` flag is present. Note that the `-e` flag is optional if there's only one script block to execute.
glenn jackman
2010-02-16 17:13:49
A:
linecount=0
while IFS= read -r LINE; do
echo "$((linecount++)) START $LINE END"
done < file
If you want to do fancier manipulation of the linecount:
linecount=0
while IFS= read -r LINE; do
let linecount++
echo "$((linecount-5)) START $LINE END"
done < file
profjim
2010-02-16 17:35:24