LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-04-2009, 08:31 AM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
Question sed help - replace line feed with different character


I'm trying to take the output of a command which is listed on many lines (for example: rpm -qa | grep xorg) and change it to one line of output and replace the line feeds with some other character. I have:

Code:
sed -e '{
N
s/\n/;/
}'
which partially works, but it only catches every other line. Does anyone have a suggestion?

Thanks!
 
Old 05-04-2009, 09:41 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

2 things:

- Can this be done with sed: Yes -> sed ':a;N;$!ba;s/\n/;/g' infile. I do believe (not my own sed oneliner) that this would involve using sed's buffer space, I'm not sure how big that space is. It could become an issue.

- Is there a better/other way: Yes -> tr '\n' ';' < infile

Hope this helps.
 
Old 05-04-2009, 09:48 AM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
It's quite easy in Perl. Just pipe the output to:
Code:
perl -e 'while(<>){chomp;print "$_;";}'
This reads each line from STDIN - while(<>), strips the newline - chomp and prints the stripped line followed by a semi-colon - print "$_;" ($_ is a Perl default variable).

QED
 
Old 05-04-2009, 09:59 AM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by bradvan View Post
I'm trying to take the output of a command which is listed on many lines (for example: rpm -qa | grep xorg) and change it to one line of output and replace the line feeds with some other character. I have:

Code:
sed -e '{
N
s/\n/;/
}'
which partially works, but it only catches every other line. Does anyone have a suggestion?

Thanks!
sed -n 'H;${g;s/\n//g;p}' filename

is the only way I know to do it using sed, but there are easier ways using tr and perl. Check it out, found this thread on LQ...

https://www.linuxquestions.org/quest...ewline-530376/
 
Old 05-04-2009, 10:31 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Another method
Code:
echo 'abc
def
ghi' | paste -sd';'
abc;def;ghi
 
1 members found this post helpful.
Old 05-04-2009, 11:40 AM   #6
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
Thanks for the replies! I had tried the tr, but I must have messed up the syntax. What you supplied worked. The sed doesn't want to work on one line. It reports label too long. If I break it up so that :a is on it's own line, then it works. The perl worked also as well as the paste command (adding - to the end). Give a task to a group of programmers and most often you will get unique answers that accomplish that task.

Thanks to all!
 
Old 04-22-2012, 06:15 PM   #7
marvs
LQ Newbie
 
Registered: Apr 2012
Posts: 3

Rep: Reputation: Disabled
Another end line removal

If test.txt is:
first
second
third

the following works for in a BASH (at least) script, so long as LIN doesn't exhaust ENVIRONMENT reserve.
#!/bin/bash
LIN=$(cat test.txt)
echo $LIN > lintest.txt
# end

cat lintest.txt should show:
first second third
 
Old 04-22-2012, 11:31 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by marvs View Post
If test.txt is:
first
second
third

the following works for in a BASH (at least) script, so long as LIN doesn't exhaust ENVIRONMENT reserve.
#!/bin/bash
LIN=$(cat test.txt)
echo $LIN > lintest.txt
# end

cat lintest.txt should show:
first second third

First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Next, this doesn't replace newlines with semicolons, as the (now 3-year-old) OP wanted. But admittedly it does remove them.

And the reason this "works" like that is because of the 'echo $LIN' line. Since the variable isn't quoted, the contents of the variable are word-split after expansion, and echo sees each word in the file as a separate value to print.

The problem with using this is that the shell splits values on all contiguous instances of whitespace, not just newlines. It also has an added danger that globbing patterns are expanded as well.

Code:
#line 4 is a blank line
$ cat file.txt
line with single spaces
line   with   multiple   spaces
line            with            tabs

line with glob *

$ text=$( <file.txt )
$ echo $text
line with single spaces line with multiple spaces line with tabs line with glob file.txt file2 file3

$ echo "$text"
line with single spaces
line   with   multiple   spaces
line            with            tabs

line with glob *
Quoting the variable preserves all whitespace and other special characters.
You could manually disable globbing and set IFS to newline only to work around most of these issues, but why bother when there are better ways to handle it, as documented above.

Incidentally, here's another method I just thought of that does replace newlines with semicolons, using bash v4's mapfile command and an array.

Code:
$ shopt -s extquote	#you may need to enable this first, esp. in scripts

$ mapfile array <file.txt

$ echo "${array[*]//$'\n'/;}"
line with single spaces; line   with   multiple   spaces; line          with            tabs; ; line with glob *;
Edit: Actually even a single variable would work; and maybe even better (I see there's an extra space inserted between the "lines" in the above, due to the default IFS setting. Set IFS to null first to fix that.):

Code:
$ text=$( <file.txt )

$ echo "${text//$'\n'/;}"
line with single spaces;line   with   multiple   spaces;line            with            tabs;;line with glob *

Last edited by David the H.; 04-22-2012 at 11:47 PM. Reason: fixed mistaakes, and as stated
 
  


Reply

Tags
awk, perl, regex, sed, tr



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
removing new line character using sed Fond_of_Opensource Linux - Newbie 4 03-26-2010 02:19 PM
Replace 2nd to last Character with SED elproducto Programming 5 03-31-2009 12:41 PM
how to search and replace character from middle of line vishal_titre Linux - General 7 09-30-2008 02:46 AM
Perl - Tpl file - Need to replace new line character. knnirmal Programming 2 09-07-2004 02:27 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:16 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
Open Source Consulting | Domain Registration