I am trying to edit a file that contains: Time Column 1 Column 2 Column 3. I need to print Column 2 after column 1.

Time Column 1
           Column 2

Recommended Answers

All 2 Replies

Wait, what? You mentioned Perl in the thread title, but then you added tags for C, C++, and Python. Which is it?

Also, the description doesn't match the illustrative example. How do you need it laid out?

Assuming you do indeed mean Perl, and that you are in shell rather than a GUI window, then there are several options (Perl always has multiple solutions). The simplest, but also most fragile, approach is to use sprintf() with a suitable format string, like so:

sprintf("%20s%20s%20s%20s", "Time", "Column_1", "Column_2", "Column_3")

This example will print the data right-justified in columns of 20 characters. However, there are a number of better approaches, such as the Text::Column library in CPAN. A quick search on CPAN for 'Column' finds a few others you could try, while a similar search on 'tabulate' finds even more, some of which would probably suit your intended result.

Correction: the strings would be left-justified, not right-justified.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.