Operating System - Linux
1827286 Members
1669 Online
109717 Solutions
New Discussion

How to append something to appointed line of ACSII file use command line?

 
SOLVED
Go to solution
NiCK_76
Respected Contributor

How to append something to appointed line of ACSII file use command line?

How to append something to appointed line of ACSII file use command line?
For example:
append " 138" to abc.txt must follow "130 131 132 133 134 135 136 137"
original abc.txt
[root@webapp home]# cat abc.txt

130 131 132 133 134 135 136 137


I need execute one or more command under command line to append content of abc.txt.

I wanna abc.txt as following:
[root@webapp home]# cat abc.txt

130 131 132 133 134 135 136 137 138
just for fun
5 REPLIES 5
Sergejs Svitnevs
Honored Contributor

Re: How to append something to appointed line of ACSII file use command line?

sed s/"137"/"137 138"/ abc.txt

Regards,
Sergejs
Sergejs Svitnevs
Honored Contributor

Re: How to append something to appointed line of ACSII file use command line?

Ooops, if you want script, simply like this:

sed s/"137"/"137 138"/ abc.txt > abc_changed.txt

mv -f abc_changed.txt abc.txt

Regards,
Sergejs
NiCK_76
Respected Contributor

Re: How to append something to appointed line of ACSII file use command line?

The abc.txt just a test file. And maybe content differently.
I just knew line number , not content.
just for fun
Bojan Nemec
Honored Contributor

Re: How to append something to appointed line of ACSII file use command line?

Nick,

Maybe this script may help (chose your favorite name):


#!/bin/bash
lin=0
while read -e line
do
let "lin += 1"
if [ "$lin" == "$1" ]; then
echo $line$2
else
echo $line
fi
done


Use
# cat abc.txt | script 2 " 138" > newfile

where 2 is line and " 138" is text to append.

Bojan
Sergejs Svitnevs
Honored Contributor
Solution

Re: How to append something to appointed line of ACSII file use command line?

Example:
line number=2
text to add=138

Script:

sed '2,2 s/$/ 138/' abc.txt > abc_changed.txt
mv -f abc_changed.txt abc.txt