LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-22-2016, 05:55 AM   #1
upkar
LQ Newbie
 
Registered: Mar 2016
Posts: 3

Rep: Reputation: Disabled
Convert one line file into a file with three columns


I have file with input like below in a single line.

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.


I need an output with only three fields in the line. Please suggest.

Required Output

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.
 
Old 03-22-2016, 06:04 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,658

Rep: Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708
Homework?

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.
 
Old 03-22-2016, 06:08 AM   #3
upkar
LQ Newbie
 
Registered: Mar 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Tanks for prompt reply. I tried using awk but the output is not correct. This format is required for one of our report.
 
Old 03-22-2016, 06:34 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by upkar View Post
Tanks for prompt reply. I tried using awk but the output is not correct. This format is required for one of our report.
Can you paste your awk command code here please?
 
Old 03-22-2016, 08:38 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
yeah what @wpeckham said.

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.

Last edited by BW-userx; 03-22-2016 at 08:39 AM.
 
Old 03-23-2016, 02:07 AM   #6
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
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....
 
Old 03-23-2016, 05:49 AM   #7
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,658

Rep: Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708
Quote:
Originally Posted by A.Thyssen View Post
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"!
 
Old 03-23-2016, 07:03 PM   #8
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
Quote:
Originally Posted by wpeckham View Post
No, you are breaking on comma. Look again at the source data. You want to break on space UNLESS there is a comma.
Which comes back to properly defining the problem before starting.
 
Old 03-23-2016, 08:14 PM   #9
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,658

Rep: Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708Reputation: 2708
On the same wavelength...

Quote:
Originally Posted by A.Thyssen View Post
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.
 
Old 03-23-2016, 08:39 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by wpeckham View Post
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

Last edited by BW-userx; 03-23-2016 at 08:40 PM.
 
Old 03-26-2016, 05:41 PM   #11
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Just in case anyone in the future sees this thread, here's one (probably inefficient) way of achieving the result that the OP wanted:

Code:
sed 's/\([a-zA-Z ]\+, [a-zA-Z.]\+ \)/\1\n/g' aaaa | awk '{getline b;getline c;printf("%s%s%s\n",$0,b,c)}'
 
Old 03-26-2016, 10:21 PM   #12
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,374

Rep: Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754
A purely awk solution.
Code:
awk '{x=y=1; while (x<=NF) {printf("%s ",$x); if (y==4) {printf("\n"); y=1}; if (index($x,",")) y+=1; x+=1}}' input.txt

Last edited by allend; 03-26-2016 at 11:07 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl one liner to delete line in csv file if one columns is toobig casperdaghost Linux - Newbie 2 01-15-2015 07:38 AM
Script to convert words in a text file [columns to rows] naveenchandar Linux - Newbie 2 11-08-2012 01:00 AM
command to write on first line of a file (as a heading for columns) in linux/solaris. Sha_unix Linux - Newbie 9 11-23-2011 02:14 PM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 09:14 PM
Convert a number on each line to CSV file OlRoy Programming 9 08-20-2008 11:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration