[SOLVED] Insert line using sed or awk at line using line number as variable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I prefer to use awk for this, since then I don't need to worry about escaping:
Code:
awk -v "n=line-number" -v "s=line to insert" '(NR==n) { print s } 1' input-file
First line is line number 1. If the file is too short, it will not insert anything.
This second variant will append the line, if the line number is larger than the number of lines in the file. It will not add empty lines, though; just the line to be inserted:
Code:
awk -v "n=line-number" -v "s=line to insert" '
(NR==n) { n=-1 ; print s }
{ print $0 }
END { if (n>0) print s }' input-file
To do the above safely to any file, I'd write a small shell script. I prefer Bash, but since I don't know which shell you prefer, here's something that should work with any /bin/sh. Note, this is untested code:
Code:
#!/bin/sh
LINENUM=55
LINESTR="inserted line"
if [ ":$*" = ":" ] || [*":$*" = ":-h" ] || [ ":$*" = ":--help" ]; then
exec >&2
echo ""
echo "Usage: $0 [ -h | --help ]"
echo " $0 filename(s)"
echo ""
exit 0
fi
WORK=`mktemp -d` || exit $?
trap "rm -rf '$WORK'" EXIT
while [ $# -gt 0 ]; do
if [ ! -f "$1" ]; then
echo "$1: File not found." >&2
exit 1
fi
awk -v "n=$LINENUM" -v "s=$LINESTR" '
(NR==n) { n=-1; print s }
{ print $0 }
END { if (n>0) print s }'
' "$1" > "$WORK/temp" || exit $?
chown --reference="$1" "$WORK/temp" 2>/dev/null
chmod --reference="$1" "$WORK/temp" 2>/dev/null
mv -f "$WORK/temp" "$1" || exit $?
echo "$1: Modified." >&2
shift 1
done
If you are not sure why it worked? It is because your previous invocation had it looking for a variable called $linei
and then passing \a which sed does not understand as an escape character.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.