1

I am a new Ubuntu user. Please help me in solving the following problem:

My input text file is

    -0.716425 -12.8939 -3.3341 -7.38497 -2.62709 3.00437
    -6.69861 -13.8853 -5.81095 -7.37465 -0.268193 -5.45344

My output text file should look like

    1:-0.716425 2:-12.8939 3:-3.3341 4:-7.38497 5:-2.62709 6:3.00437
    7:-6.69861 8:-13.8853 9:-5.81095 10:-7.37465 11:-0.268193 

I tried sed command but didn't get the required results.

How should I do that from command line?

3
  • Your desired output is missing the last number, -5.45344.
    – John1024
    Commented Apr 12, 2015 at 20:40
  • Oh!! sorry .. Yeah it should be 11: -0.268193 12:-5.45344.. Thanks @John1024 for pointing it out.
    – YUVRAJ
    Commented Apr 12, 2015 at 20:42
  • @YUVRAJ Do you want to preserve the whitespace as it exists before, between, and after the entries, or may/should it be removed? As written, your input text and desired output text both have four leading spaces at the beginning of each line. But you've commented that John1024's answer, which will not preserve the whitespace, gives the correct output. You may wish to edit your question to clarify this. Commented Apr 12, 2015 at 22:15

3 Answers 3

2
$ cat file
-0.716425 -12.8939 -3.3341 -7.38497 -2.62709 3.00437
-6.69861 -13.8853 -5.81095 -7.37465 -0.268193 -5.45344
$ awk '{for (i=1;i<=NF;i++)printf "%s:%s%s",++n,$i,OFS;print""}' file
1:-0.716425 2:-12.8939 3:-3.3341 4:-7.38497 5:-2.62709 6:3.00437 
7:-6.69861 8:-13.8853 9:-5.81095 10:-7.37465 11:-0.268193 12:-5.45344 

How it works

  • for (i=1;i<=NF;i++)printf "%s:%s%s",++n,$i,OFS

    This loops over each field on a line and prints it out with a number prepended. The variable n is the number. ++n increments n by one each time it is invoked.

  • print""

    This prints a newline at the end of each line.

To update the file in place

awk '{for (i=1;i<=NF;i++)printf "%s:%s%s",++n,$i,OFS;print""}' file >file.tmp && mv file.tmp file

Or, with the newest versions of GNU awk (4.1.0+, in Ubuntu 14.10+):

gawk -i inplace '{for (i=1;i<=NF;i++)printf "%s:%s%s",++n,$i,OFS;print""}' file
3
  • It is displaying output correctly but I want to write the output to same file. So, is it possible??
    – YUVRAJ
    Commented Apr 12, 2015 at 20:59
  • @YUVRAJ That is simple enough. See updated answer for how to update in place.
    – John1024
    Commented Apr 12, 2015 at 22:54
  • @EliahKagan Thanks. Answer updated for Ubuntu 14.10+.
    – John1024
    Commented Apr 13, 2015 at 0:06
0

If you are using GNU awk (gawk) you can accomplish this with clever record splitting:

awk '{ORS=RT; print NR":"$0}' RS='[ \n]+' infile

Output:

1:-0.716425 2:-12.8939 3:-3.3341 4:-7.38497 5:-2.62709 6:3.00437
7:-6.69861 8:-13.8853 9:-5.81095 10:-7.37465 11:-0.268193 12:-5.45344
4
  • How can I write this output to the same file?? It is displaying the output correctly but I want to overwrite it on the same file..
    – YUVRAJ
    Commented Apr 12, 2015 at 21:16
  • Is there a way to make this work even when the input has leading whitespace? Commented Apr 12, 2015 at 23:19
  • @YUVRAJ: write it to a temporary file and overwrite the original after, as suggested by John1024.
    – Thor
    Commented Apr 13, 2015 at 9:46
  • @EliahKagan: Yes, but it is not pretty, besides according to the OPs comment, it seems the white-space is there due to a formatting error. Anyway, if you really wanted to accommodate white-space at the beginning, something like this would work: file=infile; paste -d '' <(sed -r 's/^(\s*).*/\1/' $file) <(sed -r 's/^\s*//' $file | awk '{ORS=RT; print NR":"$0}' RS='[ \n]+').
    – Thor
    Commented Apr 13, 2015 at 9:48
0

How about python:

#!/usr/bin/env python2
with open('/path/to/file.txt') as f:
    prev = 0
    for line in f:
        elements = line.rstrip().split()
        for i in range(len(elements)):
            print str(prev + i + 1) + ':' + elements[i],
        print '\n'.rstrip()
        prev = len(elements)

Output:

1:-0.716425 2:-12.8939 3:-3.3341 4:-7.38497 5:-2.62709 6:3.00437 
7:-6.69861 8:-13.8853 9:-5.81095 10:-7.37465 11:-0.268193 12:-5.45344

If you want to save the output to the same file, you should save the output to a temporary file first, then remove the original file and then rename the temp file to original file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .