LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-10-2011, 05:28 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Bash; awk or sed output to variable: how keep newline at end of each output line


Either of
Code:
txt=$(sed -n -e "${ln[0]},${ln[1]} p" -e "${ln[1]} q" Notes)
txt=$(awk -v x=${ln[0]} -v y=${ln[1]} 'NR>x, NR<y' < Notes)
does what I want except all the newlines are removed if their output goes to a variable. How can I preserve them?
 
Old 06-10-2011, 05:37 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Have you tried to embed the variable in double quotes?
Code:
echo "$txt"
 
1 members found this post helpful.
Old 06-10-2011, 05:46 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by colucix View Post
Have you tried to embed the variable in double quotes?
Code:
echo "$txt"
Aah, thank you. So they are there, its just that echo $txt shows line breaks as spaces.
 
Old 06-10-2011, 05:50 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This is the way echo works: it outputs the arguments separated by spaces. Multiple lines are multiple arguments separated by newlines, whereas using double quotes multiple lines are treated as a single argument containing newlines and it is printed as is.
 
  


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
sed output -> variable problem figo Programming 13 05-31-2009 09:26 AM
AWK/Sed do not produce any output MasterOfTheWind Linux - Newbie 6 05-24-2009 07:50 AM
how to pipe/redirect awk output into a variable? johnpaulodonnell Linux - Newbie 2 01-25-2007 06:54 AM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM

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

All times are GMT -5. The time now is 09:43 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