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.
easier:
cat distros.txt | xargs -n 100
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.
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!
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.
cat file.txt | xargs
benefit.
I always end up using excel for this [transpose] ... very useful
Another variant:
cat distros.txt | tr «\ n» »«
HaHaHa in each comment, a different way of doing the same!
And to do the opposite, what would it be like? That is, converting a line of words separated by spaces into a column.
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
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!
Definitely Etemenanki's solution, both ways, is fine if you only want a simple transformation from row to column and vice versa.
Thank you all for responding. I'll put them into practice in the next script I do.
Greetings.
And if you only do:
echo $ (cat distros.txt)
Even easier:
awk '{printf $ 0 ″ «}' distros.txt