Ravi kumar
Ravi kumar

Reputation: 129

indent lines equally with echo

I am struggling to cat a file while print it along with some words.

For example:

/root/file contains:

line1
line2
line3

Script:

#!/bin/bash
echo -en "Printing Line of the file: `cat /root/file`\n"

Result:

Printing Line of the file: line1
 line2
 line3

Expected Result:

Printing Line of the file:  line1
                            line2
                            line3

How can I get the output I want?

Upvotes: 3

Views: 8279

Answers (4)

Michael Back
Michael Back

Reputation: 1871

The following prints line by line -- right justify at 32 characters:

awk '{ printf("%32s\n", $0) }'

Assuming same length lines... we could peak at first line, count characters and set the pattern for printf on all lines.

awk 'NR==1 { f = "%" length "s\n" } { printf(f, $0) }'

If lines are not the same length, the same idea can be used with sprintf to create a pad of spaces:

awk -v x='Printing Line of the file:  ' 'NR==1 { pad = sprintf("%" length(x) "s", ""); print x $0; next } { print pad $0 }' /root/file

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 247022

Just bash:

( 
    first="Printing Line of the file:"
    IFS=
    read -r line
    printf "%*s %s\n" ${#first} "$first" "$line"
    while read -r line; do 
        printf "%*s %s\n" ${#first} "" "$line"
    done 
) < file
Printing Line of the file: line1
                           line2
                           line3

With printf, you can use * as the field width, then provide a number in the arguments. I'm running this in a subshell so altering IFS does not affect the parent shell.


To achieve your written goal with the tabs:

echo "Printing Line of the file: $(awk -v ORS="\n\t\t\t" 1 file)"

Upvotes: 7

Kalenda
Kalenda

Reputation: 1927

You can use existing utilities like this:

myvar="Printing Line of the file:  "
size=${#myvar}
# getting the right size is the tricky part
var_final_size=$((size+5))
echo -en "${myvar}`awk -F\; -v fmt="%${var_final_size}s\n" '{if (NR==1) {print $1} else {printf fmt, $1}}'  test.txt`\n"

Or you can script something like this:

#!/bin/bash
myvar="Printing Line of the file:  "
size=${#myvar}
var_counter=0
for r in  `cat /root/file`
do 
   var_size=${#r}
   var_final_size=$((size+var_size))
   if [ $var_counter -eq 0 ]; then
      printf "%${var_final_size}s\n" "${myvar}${r}"
   else 
      printf "%${var_final_size}s\n" "${r}" 
   fi 
   var_counter=$((var_counter+1))
done

Output

Printing Line of the file: line1
                           line2
                           line3

Upvotes: 1

TessellatingHeckler
TessellatingHeckler

Reputation: 29033

echo -en "Printing Line: `awk '{if (NR==1) {print " "$0} else {print "\t\t\t"$0}}' file`\n"

Printing Line:  line1
                        line2
                        line3

(That seems to work, even though it's nesting " inside "", it might need to be print \" \"$0 and similar to escape them properly, I don't know).

Upvotes: 0

Related Questions