Question

I'm new on shell scripting, and I need a help on this.

How could I create an output file delimited by variable strings? I mean, each variable has an specific length, for example:

variable1 (char 10)
variable2 (char 5)
variable3 (char 4)
variable4 (char 7)
variable5 (char 20)
variable6 (char 2)
...

The output file would be like this (each number is related to the content of the respective variable):

11111     22222333344444445555555             66

The content of next variable starts on the next position in the file, i.e., variable2 starts at 11th position, because the variable1 ends at 10th position. And the remaining space in the variable would be filled with blank spaces.

Was it helpful?

Solution

Use printf to print the strings:

$ printf "%-10.10s%-5.5s%-4.4s%-7.7s%-20.20s%-2.2s\n" 11111 22222 3333 4444444 5555555 66
11111     22222333344444445555555             66

For instance, %-10.10s indicates the corresponding argument should be left-justified (-) in a field exactly 10 characters wide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top