- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Join lines into one line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 05:20 AM
тАО04-07-2011 05:20 AM
I have a text file which includes undefined number of lines as follows:
====file.txt====
line1
line2
line3
line4
[...]
lineN
============
What command should I use to have the output like that:
line1,line2,line3,line4,[...],lineN
awk is very welcome. Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 05:38 AM
тАО04-07-2011 05:38 AM
Re: Join lines into one line
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 05:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 05:48 AM
тАО04-07-2011 05:48 AM
Re: Join lines into one line
# cat file.txt | xargs
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 05:49 AM
тАО04-07-2011 05:49 AM
Re: Join lines into one line
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 05:49 AM
тАО04-07-2011 05:49 AM
Re: Join lines into one line
# cat file.txt | xargs | tr " " ","
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 06:11 AM
тАО04-07-2011 06:11 AM
Re: Join lines into one line
here is your AWKward solution:
# awk '{printf $0","} END {print}' file.txt
Unix operates with beer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 06:14 AM
тАО04-07-2011 06:14 AM
Re: Join lines into one line
Not so good if the lines contain spaces:
dyi # cat file.txt
line 1
line 2
line 3
dyi # cat file.txt | xargs | tr " " ","
line,1,line,2,line,3
But (this time on a real HP-UX system):
dyi # ( tr '\n' ',' ; echo '' ) < file.txt | sed -e 's/,$//'
line 1,line 2,line 3
(Some "sed" programs don't do well with
unterminated lines, I see. Everything's
complicated.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 06:17 AM
тАО04-07-2011 06:17 AM
Re: Join lines into one line
Around here, that still has the extra comma
at the end:
dyi # awk '{printf $0","} END {print}' file.txt
line 1,line 2,line 3,
(But it does better with embedded spaces.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-07-2011 06:31 AM
тАО04-07-2011 06:31 AM
Re: Join lines into one line
at the end
nope, on my linux box it duplicates the last line and so I didn't end up with an extra comma. I just didn't recognize the duplicate.
basically I wanted this version, but somehow the reference to the last line didn't work:
# awk 'NR != "$NR" {printf $0","} END {print}' test
so I ended up with this:
# awk '!(NR-1) {printf $0} NR-1 {printf ","$0} END {print ""}'
which works on linux. (too sad I don't have an HP-UX access at the moment :(
Unix operates with beer.