194. Transpose File

Transpose File

Transpose File

Problem Description

Given a text file file.txt, transpose its content.

You may assume that each row has the same number of columns, and each field is separated by the ' ' character.

Example:

If file.txt has the following content:

name age
alice 21
ryan 30

Output the following:

name alice ryan
age 21 30


Solution:

# Read from the file file.txt and print its transposed content to stdout.
seq "$(awk '{print NF}' file.txt | head -n 1)" |
	xargs -r -I {} sh -c "awk '{print \${}}' file.txt | xargs -r"

Conclusion


That’s all folks! In this post, we solved LeetCode problem #194. Transpose File

I hope you have enjoyed this post. Feel free to share your thoughts on this.

You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.


In this blog, I have tried to solve leetcode questions & present the most important points to consider when improving Data structure and logic,  feel free to add, edit, comment, or ask. For more information please reach me here
Happy coding!

Comments

Popular Post