LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-05-2024, 10:13 PM   #1
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 31

Rep: Reputation: 4
cat using readline(read -e)


Is it possible to get cat, in this example, to use readline(read -e) to process/edit its input?

Code:
cat > datafile
What I'd like to see cat > datafile do is:

Code:
#! /usr/bin/env bash
# -e engages readline
while read -r -e line
do
    echo "${line}"
done
 
Old 05-06-2024, 01:26 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,874
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Don't really understand the problem, but try this:
Code:
#! /usr/bin/env bash
# -e engages readline
while read -r -e line
do
    echo "${line}"
done >datafile
Mind you, echo is incompatible (you cannot really tell what will happen if "line" is "-n", the compatible version is printf:
Code:
printf '%s\n" "$line"

Last edited by NevemTeve; 05-06-2024 at 01:28 AM.
 
1 members found this post helpful.
Old 05-06-2024, 01:40 AM   #3
___
Member
 
Registered: Apr 2023
Posts: 155
Blog Entries: 1

Rep: Reputation: Disabled
Wow: man readline(3) is 1114 lines long (+ http://www.gnu.org/software/readline)!

No: the cat(1) program contains no such logic=functionality. You would have to use your script.

p.s. #2 (good point!) needs edit: choose either matching ' or " for printf '%s\n" format string!

Last edited by ___; 05-06-2024 at 02:54 AM.
 
1 members found this post helpful.
Old 05-06-2024, 01:58 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,974

Rep: Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335
yes, would be nice to see more details, why do you want to read a line and put it into a file (in a cycle)? Use an editor to create the content and save it.
 
Old 05-06-2024, 03:25 AM   #5
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 31

Original Poster
Rep: Reputation: 4
Since
Code:
cat > datafile
Provides a simple way to create a text file, I thought there must/should be a simple way to engage readline(in this usage) too.
 
Old 05-06-2024, 03:37 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,974

Rep: Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335Reputation: 7335
to create a file you need not use cat, but touch. Or just simply
Code:
> datafile
Otherwise I still don't know how is it related to readline or a while loop.
 
Old 05-06-2024, 03:47 AM   #7
___
Member
 
Registered: Apr 2023
Posts: 155
Blog Entries: 1

Rep: Reputation: Disabled
Found rlwrap(1) (may need apt install)

I just found something! (Let me/us know if it works (for what you want, since you don't want to simply use any editor program, per #4)

rlwrap cat >file
 
2 members found this post helpful.
Old 05-06-2024, 03:53 AM   #8
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 31

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by ___ View Post
I just found something! (Let me/us know if it works (for what you want, since you don't want to simply use any editor program, per #4)

rlwrap cat >file
Yes! That's exactly what I was looking for.
 
1 members found this post helpful.
Old 05-06-2024, 07:22 AM   #9
mjolnir
Member
 
Registered: Apr 2003
Posts: 824

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by ___ View Post
I just found something! (Let me/us know if it works (for what you want, since you don't want to simply use any editor program, per #4)

rlwrap cat >file
Interesting. Essentially creates a mini-text editor on the spot
 
Old 05-06-2024, 02:44 PM   #10
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 31

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by mjolnir View Post
Interesting. Essentially creates a mini-text editor on the spot
A mini-text editor that has switches to modify how it behaves:

Quote:
...
-c, --complete-filenames
Complete filenames (filename completion is always case-sensitive, even with the -i option) When doing this, rlwrap keeps track command's working directory is.
-C, --command-name <command_name>|<N>
Use command_name instead of command to determine the names of history and completion files, and to initialise readline (as specified in ~/.inputrc). A numeric argument N > 0 means: use the Nth argument counting backwards from the end of the argument list
...
 
2 members found this post helpful.
Old 05-07-2024, 03:17 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
So I am curious if you could just have done a here-doc?
Code:
cat >your_file<<EOF
type stuff
in here
EOF
 
1 members found this post helpful.
Old 05-07-2024, 03:46 AM   #12
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 31

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by grail View Post
So I am curious if you could just have done a here-doc?
Code:
cat >your_file<<EOF
type stuff
in here
EOF
I suppose you could(since the shell can support readline) but I find rlwrap a cleaner solution.
 
  


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
[SOLVED] [Question]How to make GNU readline read until balanced parenthesis string is matched? oren_daniel Programming 2 09-19-2020 01:39 PM
Patch for readline.SlackBuild to include rlfe (ReadLine Fron-End) tfonz Slackware 0 10-22-2016 11:45 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
Using cat readline within cat readline while loop demxkn66 Programming 5 12-16-2007 05:10 PM
cat in asm/ cat --show-all option Tux Linux - Software 1 09-02-2006 09:31 PM

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

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