1

I have 3 files ex- abc.txt, def.txt & xyz.txt. I've generate one shell script where I want to content of these 3 files should appear line wise like as below-

abc ---- 1 2 3 4 5 6 7 def ---- 3 5 7 9 11 13 15 xyz ---- 4 8 12 16 20 24 28 

Also the content of the files should be like this manner & every value in parallel wise. Could you please help me to generate the shell script. But in actual when i run script then it is showing like-

abc ---- 1 2 3 4 5 6 7 def ---- 3 5 7 9 11 13 15 xyz ---- 4 8 12 16 20 24 28 

So I want to arrange like above content of the output. Where I used in the script for the above requirement-

 for f in abc.txt def.txt xyz.txt; do (cat "${f}"; echo) >> output.txt; done |awk '{printf "%-10s|%-10s|%-10s|%-10s|%-10s|%-10s|%-10s\n",$1,$2,$3,$4,$5,$6,$7} output.txt > final_output.txt 

Is it good approach to generate the script

4
  • Please give details on how the content of your files looks like. Are all numbers in different lines in these files, or are they all on the same line, or mixed? Commented Nov 7, 2019 at 9:57
  • @AdminBee I've updated please see. Actually I want the output in above format. Commented Nov 7, 2019 at 10:02
  • @MdWasi The question is what the input (e.g. abc.txt) looks like, though. Commented Nov 7, 2019 at 11:46
  • @DonHolgo After run the shell script, arrangement of the data like that I am getting which mentioned above in the question is not as expectation. Commented Nov 8, 2019 at 10:22

2 Answers 2

2

You can use the columns command

./your_script | column -t -s' ' 

This will be close to your desired output but the numbers would not be right-justified

abc ---- 1 2 3 4 5 6 7 def ---- 3 5 7 9 11 13 15 xyz ---- 4 8 12 16 20 24 28 

To right justify the numbers just pipe the output of your shell script to rev and columns command like this

./your_script | rev | column -t -s' ' | rev 
  • The first rev will reverse every line
  • Then the columns command will create columns with space as the delimiter
  • Finally , the last rev will revert back the original line - giving you right aligned numbers.
1
  • Thanks a lot for the solution. But arrangement that I want to get as you mentioned above it is not showing like that. Commented Nov 8, 2019 at 10:22
0

I think you should use @amisax' answer to rewrite your script to remove redundancies. The following should do what you want:

#!/bin/bash for f in abc.txt def.txt xyz.txt do echo "$f --- " "$(cat $f)" >> ct_output.txt done column -t -s' ' ct_output.txt 

This creates a temporary file in which the every line contains the name of the input file, followed by its content (assuming the content is one one line only), and pipes this file through column as explained by @amisax. This should yield the desired

user@host $ ./columnize.sh abc.txt --- 1 2 3 4 5 6 7 def.txt --- 3 5 7 9 11 13 15 xyz.txt --- 4 8 12 16 20 24 28 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.