0

I have a file named parse.txt that contains the following 2 lines:

60

Moderate

I'm hoping to have a command in a bash script that will insert the 60 into line 7, column 17 of a new final.txt file, and insert the Moderate into line 8, column 53 also in the final.txt file.

  • 1
    Seems arbitrary. Have you tried anything to accomplish this yet? What delimits the columns in final.txt? – Jesse_b Jun 13 at 18:12
  • final.txt is in table format, with two of the "table columns" starting at column number 17, and the other column number 53. – Matt Jun 13 at 18:24
  • I'm unfamiliar with table format – Jesse_b Jun 13 at 18:30
  • I'm not sure table format was the right description, I guess what I'm trying to say is I understand how to copy things from one file to another on a particular line, but not sure how to insert this by column also. – Matt Jun 13 at 18:33
  • How are the columns delimited? With a space, a comma, a tab, something else? I'm also confused by where you said that one of the columns starts at column 17. Is it the first column or the 17th column? – Jesse_b Jun 13 at 18:33
0

You could use this simple script:

#!/bin/sh
num=$(head -1 parse.txt)
val=$(tail -1 parse.txt)

# add 6 blank lines
for i in $(seq 6); do
  printf '\n' >> final.txt
done

# add 16 blank columns
for i in $(seq 16); do
  printf ' ' >> final.txt
done

# add num to line 7, column 17
echo $num >> final.txt

# add 52 blank columns
for i in $(seq 52); do
  printf ' ' >> final.txt
done

# add val to line 8, column 53
echo $val >> final.txt

So, for example, in vim, when selecting the value it will show: "final.txt" line 7 of 8 --87%-- col 17

  • Thanks, if I want them both in line 7, how would this be changed? – Matt Jun 13 at 19:13
  • You would modify echo $num >> final.txt to prevent adding a new line via printf $num >> final.txt and then change for i in $(seq 52); do to for i in $(seq $((36-${#num}))); do – jjj Jun 13 at 19:21
  • Did this sufficiently answer your question? – jjj Jun 14 at 6:22
  • One lingering issue is I'm trying to add an additional piece of data to the same line in a different column...a 15 at column #60. I tried out an additional "for i in $(seq $((36-${#num}))); do" but it forces the value on a new line, not the same line. – Matt Jun 14 at 17:26
  • Because I need three pieces of data on the line, not two – Matt Jun 14 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.