1

I have a .txt file (new_file.txt) with a column of values (200). I need to print another column beside it, with values 0,1/200,2/200.....1. How should I do this? I am new to this, so any suggestions will be great!

I know that seq 0 0.005 1 >new_file.txt will print it into the file, but it overwrites the values already present. I want to add these numbers as another column beside the values already present in the file.

The input is like:

2.41
2.56 

etc. in a column. I need it to look like

2.41 0
2.56 0.005

etc. in a column. I need to have a tab in between.

1
  • 1
    use paste or pr command... suggest to show small sample (say 5 lines) for input and how the output should look after column addition.. should it be added with space/tab/etc in between or without any separation? – Sundeep Jul 2 '17 at 2:55
1

With seq and paste:

seq 0 0.005 1 | paste newfile.txt - > newerfile.txt

With awk

awk '{$2 = 0.005*(NR-1)} 1' OFS='\t' newfile.txt > newerfile.txt

Depending on your version of awk, you may be able to modify newfile.txt in place.

1

As mentioned in comments, paste is the best option to do what you want.

 paste new_file.txt <sequence file>

If you want to generate sequence at run time

seq 0 0.005 1 | paste new_file.txt /dev/stdin

Example (for 5 records in new_file.txt)

~$ seq 0 0.005 0.020 | paste new_file.txt /dev/stdin
2.41    0.000
2.56    0.005
2.71    0.010
2.86    0.015
3.01    0.020

Note: If any of the file/command have extra rows in it, then corresponding rows in output would be blank. So make sure that both files have same number of row.

0

You can do it using GNU dc as follows:

< new_file.txt tr -- - _ | dc -e "[q]sq [?z1=qrd1<qrn32anp0.005+dd=?]s? 0l?x"

Explanation:

dc -e '
# macro for quitting
[q]sq

# macro to read next line and perform operations
[
   ? z1=q  # read next line and quit when it is empty. The 1 is apriori
   r       # else, reverse the stack elements so that sum is top of stack now
   d1<q    # quit if current sum is more than 1
   r       # else, reverse the stack elements so that line is top of stack now
   n 32an p # print the line, space (32a is ascii decimal for space), & print current sum
   0.005+   # update the current sum for next round
   dd=?     # recursively invoke itself for more.... its a loop essentially
]s?

# initialize stack and start operations
0 l?x
'

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.