Bash: Convert a column of text to a row

Suppose we have a text file called distros.txt with the following:

archlinux
debian
ubuntu
kaos
fedora
Slackware
gentoo

And we want to turn it into:

archlinux debian ubuntu kaos fedora slackware gentoo

To achieve this we will use a for loop or with a echo -n :

for i in `< distros.txt`; do echo -n ${i}" ";done; echo ""

Done, this does the trick 🙂

This will show us the desired result in the terminal, if on the other hand we want it to be saved in another .txt file we redirect the output:

for i in `< distros.txt`; do echo -n ${i}" ";done; echo "" > distros-nuevas.txt

And voila 🙂

Well nothing, I hope you find it useful. Valid to clarify that it can also be done with regular expressions, I just don't know how ... but, with regular expressions you can do almost everything hahaha.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Joseph Ricardo said

    easier:

    cat distros.txt | xargs -n 100

  2.   ¿ said

    If in an .odt I have 2 columns on each page, they read like this:

    p. 1
    col.1 col.2
    p. 2
    col.3 col.4
    etc.

    How do I get each column under the other?
    col. 1
    col. 2
    col. 3
    col. 4
    etc.

  3.   john said

    Much easier:

    If you want it separated by tabs:
    You write: paste -s distros.txt
    You get: archlinux debian ubuntu kaos fedora slackware gentoo

    If you want it separated by spaces:
    You write: paste -s -d »» distros.txt
    You get: archlinux debian ubuntu kaos fedora slackware gentoo

    If you want it separated by commas:
    You write: paste -s -d, distros.txt
    You get: archlinux, debian, ubuntu, kaos, fedora, slackware, gentoo

    With paste, cat, awk and other friends, with a little ingenuity you can make many useful combinations without complicating your life.

    Let everything continue great, congratulations on the award in Portalprogramas!

    1.    dhunter said

      I've been using sed, awk, cut, sort, uniq, in short the cream, but I had never paid attention to paste, thanks for showing what it can do. Slds.

  4.   tabris said

    cat file.txt | xargs

    benefit.

  5.   Cristianhcd said

    I always end up using excel for this [transpose] ... very useful

  6.   Bruno cascio said

    Another variant:

    cat distros.txt | tr «\ n» »«

  7.   Joaquin said

    HaHaHa in each comment, a different way of doing the same!

  8.   Jose GDF said

    And to do the opposite, what would it be like? That is, converting a line of words separated by spaces into a column.

    1.    john said

      Easy too, this time with awk.
      Assuming that the fields are separated by tabs or spaces, which is the most common (if it is by commas or something else, it must be taken into account and indicated), and that the file now contains: archlinux debian ubuntu kaos fedora slackware gentoo

      Since awk directly gives us the field we want, we have to make it iteratively show us one by one until the end. There are 7 fields because this is the value of NF (Number of fields). We set the counter i = 1, so that it shows us the first field ($ 1) and we have to get it to increase by one unit (using i ++) without exceeding the last field (NF).

      awk '{for (i = 1; i <= NF; i ++) {print $ i}}' distros.txt

      1.    Etemenanki said

        The most practical way, easy to remember and that works for both senses is this:
        cat distros.txt | tr '\ n' '' ← Horizontal stdout output (Already discussed above)
        cat distros.txt | tr '' '\ n' ← Vertical stdout output

        Regards!

    2.    john said

      Definitely Etemenanki's solution, both ways, is fine if you only want a simple transformation from row to column and vice versa.

    3.    Jose GDF said

      Thank you all for responding. I'll put them into practice in the next script I do.

      Greetings.

  9.   Cat said

    And if you only do:
    echo $ (cat distros.txt)

  10.   Anson rodriguez said

    Even easier:
    awk '{printf $ 0 ″ «}' distros.txt