0

I am using mostly one liners in shell scripting.

If I have a file with contents as below:

1
2
3

and want it to be pasted like:

1 1
2 2
3 3

how can I do it in shell scripting using python one liner?

PS: I tried the following:-

python -c "file = open('array.bin','r' ) ; cont=file.read ( ) ; print cont*3;file.close()"

but it printed contents like:-

1
2
3
1
2
3
martineau
  • 119,623
  • 25
  • 170
  • 301

6 Answers6

1
file = open('array.bin','r' )
cont = file.readlines()
for line in cont:
    print line, line
file.close()
Anomitra
  • 1,111
  • 15
  • 31
0

You could replace your print cont*3 with the following:

print '\n'.join(' '.join(ch * n) for ch in cont.strip().split())

Here n is the number of columns.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

You need to break up the lines and then reassemble:

One Liner:

python -c "file=open('array.bin','r'); cont=file.readlines(); print '\n'.join([' '.join([c.strip()]*2) for c in cont]); file.close()"

Long form:

file=open('array.bin', 'r')
cont=file.readlines()
print '\n'.join([' '.join([c.strip()]*2) for c in cont])
file.close()

With array.bin having:

1
2
3

Gives:

1 1
2 2
3 3
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

Unfortunately, you can't use a simple for statement for a one-liner solution (as suggested in a previous answer). As this answer explains, "as soon as you add a construct that introduces an indented block (like if), you need the line break."

Here's one possible solution that avoids this problem:

  1. Open file and read lines into a list
  2. Modify the list (using a list comprehension). For each item:
    • Remove the trailing new line character
    • Multiply by the number of columns
  3. Join the modified list using the new line character as separator
  4. Print the joint list and close file

Detailed/long form (n = number of columns):

f = open('array.bin', 'r')
n = 5
original = list(f)
modified = [line.strip() * n for line in original]
print('\n'.join(modified))
f.close()

One-liner:

python -c "f = open('array.bin', 'r'); n = 5; print('\n'.join([line.strip()*n for line in list(f)])); f.close()"
Community
  • 1
  • 1
jdavcs
  • 111
  • 1
  • 3
0
REPEAT_COUNT=3 && cat contents.txt| python -c "print('\n'.join(w.strip() * ${REPEAT_COUNT} for w in open('/dev/stdin').readlines()))"
cjhanks
  • 526
  • 4
  • 4
  • While answers are always appreciated, it really helps to provide some information about how your code solves the problem at hand. Please provide some context surrounding your answer, and see the [help article](http://stackoverflow.com/help/how-to-answer) for information on how to write great answers :) – Obsidian Age Feb 19 '17 at 22:42
0

First test from the command propmt:

paste -d" "  array.bin array.bin

EDIT:
OP wants to use a variable n to show how much columns are needed. There are different ways to repeat a command 10 times, such as

for i in {1..10}; do echo array.bin; done
seq 10 | xargs -I -- echo "array.bin"
source <(yes echo "array.bin" | head -n10)
yes "array.bin" | head -n10

Other ways are given by https://superuser.com/a/86353 and I will use a variation of

printf -v spaces '%*s' 10 ''; printf '%s\n' ${spaces// /ten}

My solution is

paste -d" " $(printf "%*s" $n " " | sed 's/ /array.bin /g')
Community
  • 1
  • 1
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • Hello Walter, the paste command works. The intention here is to paste the files as many number of times the files as given by a variable "n" whose value is being decided on the fly..... Thanks – user7589566 Feb 21 '17 at 18:03
  • Edited with a solution with `$n`. – Walter A Feb 21 '17 at 18:34