LinuxQuestions.org
Visit Jeremy's Blog.
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 06-18-2015, 02:34 PM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 47

Rep: Reputation: Disabled
bash move line csv format


greetings i have a file like this

abc/qwe/123/ert
Jun 18 2015 13:24:03 EDT
adc/gwe/153/poi
Jun 18 2015 14:24:03 EDT
etc...

i want to format it like this like a csv comma separated list.

abc/qwe/123/ert,Jun 18 2015 13:24:03 EDT
adc/gwe/153/poi,Jun 18 2015 14:24:03 EDT

suggestions please
Thanks again,.
 
Old 06-18-2015, 02:50 PM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 757
Blog Entries: 3

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
I am sure there are tons of ways to do this, but here's what I came up with quickly:
Code:
tr '\n' ',' < csv.txt | sed 's/T,/T\n/g'
abc/qwe/123/ert,Jun 18 2015 13:24:03 EDT
adc/gwe/153/poi,Jun 18 2015 14:24:03 EDT
Where 'csv.txt' is a file containing your original example text. Tested under Debian 8.

Edit (explanation):
tr '\n' ',' removes ALL newlines and replaces them with a comma.
sed 's/T,/T\n/g' replaces each 'T,' with a T followed by a newline (\n).

This is obviously far from foolproof, but works in this specific case.

Last edited by HMW; 06-18-2015 at 02:54 PM.
 
1 members found this post helpful.
Old 06-18-2015, 03:40 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Alternative:
Code:
awk 'ORS = NR%2?",":"\n"' file
Please remember in the future to at least show an attempt at the solution. I only present the above as a solution was already provided, but like others prefer to offer solutions
based on helping expand what you have already done.

Last edited by grail; 06-19-2015 at 03:41 PM.
 
Old 06-18-2015, 08:03 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,060

Rep: Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916
Missing a modulo operator grail ?.
 
Old 06-19-2015, 03:25 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
hmmm ... it was there when I copied it and I do not seem to be able to add it now either ... ie I add it but when I click save it seems to disappear???

Is this more weirdness with the code tags? I saw another post mentioning that capitals were not displaying in code tags
 
Old 06-19-2015, 03:42 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,527

Rep: Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439Reputation: 2439
try [noparse]text%here[/noparse]
 
Old 06-19-2015, 11:25 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Hi pan64, is this now a requirement to get a percent sign to appear? Also, am I placing the whole in code tags and just the percent in noparse or am I forgoing code tags all together??
If the solution was larger and more complex will the noparse option maintain formatting and so on as the code tags do?
 
Old 06-19-2015, 11:30 AM   #8
brianL
LQ 5k Club
 
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Slackware & Slackware64 14.1
Posts: 7,453
Blog Entries: 55

Rep: Reputation: Disabled
Another way:
Use the Go Advanced button in the Quick Reply box to get a % sign.
 
Old 06-19-2015, 11:37 AM   #9
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,466

Rep: Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371Reputation: 1371
The percent thing is still an on-going issue. http://www.linuxquestions.org/questi...gs-4175538910/
 
Old 06-19-2015, 03:42 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Thanks allend ... I went advanced to get it in
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Cursor does not move to the bottom line and overwrite to same line on command line mesuutt Linux - Newbie 3 02-25-2012 09:04 AM
Bash script to move first line to end? mac101 Programming 5 01-10-2012 02:44 AM
Putty using csv files in BASH to format file in script as output smks Linux - Newbie 7 12-22-2011 06:50 PM
In a bash script, how do I move a line to a new file. wonderfullyrich Programming 18 04-30-2011 12:52 AM


All times are GMT -5. The time now is 02:00 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration