given a plain text file, how can I do, using bash开发者_JAVA百科, awk, sed, etc, to, at the line number NLINE, add the string STR, just n spaces after the end of the line?
So, for instance, if this is the line NLINE:
date march 13th
for 5 spaces, we get
date march 13th STR
and one gets the modification in a new file.
Thanks
NLINE=2
s="somestring"
sp=" "
awk -vn="$NLINE" -vs="$s" -vsp="$sp" 'NR==n{$0=$0 sp s}1' file >temp
mv temp file
$ NLINE=666
$ APPEND=" xxx"
$ sed "${NLINE}s/\$/${APPEND}/" FILENAME
Just be careful that APPEND does not contain any characters sed might interpret.
#!/bin/bash
NLINE=10
n=5
string="STR"
while read -r line
do
if (( ++count == NLINE ))
then
printf "%s%${n}s%2\n" "$line" " " "$string"
else
echo "$line"
fi
done < inputfile > outputfile
精彩评论