Convert one line file into a file with three columns
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So, how are you approaching this? What have you tried?
If you are planning to use a shell script, what shell do you use?
What is special about three columns, why not one?
What are your restrictions?
We need to know your boundary conditions and assumptions.
with no "I have tired this and this and this". Showing your work too, and the "but it does not work please help" out of you. Just the teachers assignment for others to do for you? hummm perhaps an ethics class too is in order.
Input is all one line... Okay, first make each ',' entry a seperate line, then join every 3 lines to one line
Code:
sed 's/, */,\n/g' input.txt | sed 'N;N;N; s/\n//g' > output.txt
PS: I originally tried to use "join -t\ - - -" for the second command, which would have worked years ago but now ignores the second and later '-' to read stdin. Arrgghhhh....
Input is all one line... Okay, first make each ',' entry a seperate line, then join every 3 lines to one line
Code:
sed 's/, */,\n/g' input.txt | sed 'N;N;N; s/\n//g' > output.txt
PS: I originally tried to use "join -t\ - - -" for the second command, which would have worked years ago but now ignores the second and later '-' to read stdin. Arrgghhhh....
No, you are breaking on comma. Look again at the source data. You want to break on space UNLESS there is a comma.
I could solve this with pure bash (no awk) but it would hardly be elegant.
Because the source data seems explicitly formatted to support it, you could work with simple string locations (break into sets of six) and have it work. Not very general, and it would fail like crazy on something like "Sault Saint Marie, Michigan"!
Which comes back to properly defining the problem before starting.
Exactly. If this IS homework (and that is what it look like) it is poorly designed to reflect real world problems, but well designed to make the student THINK about the data and deal with the existing structure.
The very structure presented gives me several ideas of non-general ways to address the question, but I am still waiting to see what the OP came up with that did not work.
Exactly. If this IS homework (and that is what it look like) it is poorly designed to reflect real world problems, but well designed to make the student THINK about the data and deal with the existing structure.
The very structure presented gives me several ideas of non-general ways to address the question, but I am still waiting to see what the OP came up with that did not work.
Code:
1 2 3
city state | city state city state
Albany, | N.Y. | Albuquerque, | N.M. | Anchorage, | Alaska
Asheville,| N.C. | Atlanta, | Ga. | Atlantic City, | N.J.
Austin, | Texas | Baltimore, | Md. | Baton Rouge, | La.
Billings, | Mont. | Birmingham, | Ala. | Bismarck, | N.D.
Boise, | Idaho | Boston, | Mass. | Bridgeport, | Conn.
that's be the only logical ending format but it actually equals 6 if you split the city state .. strange assignment
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.