Nikhilesh Sharma
Nikhilesh Sharma

Reputation: 151

How to copy text from a given line and column number and paste it in another file at a given line and column number

I have a file address.txt containing
`0x0003FFB0'
at line 1 and column 2
i want to paste it in another file 'linker.txt' at line 20 and column 58
How can i do that using bash scripting?
Note that the content of the input file can be random , doesn't have to be same each time.
But the length of the word to be copied will always be the same

Upvotes: 1

Views: 1067

Answers (2)

redcurry
redcurry

Reputation: 2497

You can use a combination of head and tail to get any line number. To get line 2, get the last line (using tail) of the first two lines (using head):

ADDRESS=$(head -2 address.txt | tail -1 | cut -f2)

For the third line:

ADDRESS=$(head -3 address.txt | tail -1 | cut -f2)

And so on.

Upvotes: 0

Tiago Peczenyj
Tiago Peczenyj

Reputation: 4623

you can use SED

sed -i.old '20s/^.{58}/&0x0003FFB0/' file

it will produce a file.old with the original content and file will be updated with this address. Quickly explain

sed '20command'    file  # do command in line 20
sed '20s/RE/&xxx/' file  # search for regular expression, replace by the original text (&) + xxx

to read the address and put in this sed, cut it possible

ADDRESS=$(head -1 address.txt | cut -f2)
sed -i.old "20s/^.{58}/&${ADDRESS}/" file 

Upvotes: 2

Related Questions