开发者

Modify an exact line number of a text file and add certain string after the end of the line

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜